Any one know the easiest way to only allow numbers into a text box, I saw a reply to someone elses post a few weeks ago but didn't bother to note it, it was only about 1 line of code.
Thanks in advance!!! :)
Printable View
Any one know the easiest way to only allow numbers into a text box, I saw a reply to someone elses post a few weeks ago but didn't bother to note it, it was only about 1 line of code.
Thanks in advance!!! :)
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8
' Backspace OK.
Case 13
' Enter treated like Tab.
Text2.SetFocus
Case 48 To 57
' numbers only
Case Else
' Reject all else.
KeyAscii = 0
End Select
End Sub
I did one similar to that Larryn but using the vbconstants and it didn't work?
But I'll give your code a go...Thanks!!!
I solved my one and tried yours Larryn. Thanks for your help!!!
Code:'allow only numeric data in a textbox
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
[Edited by HeSaidJoe on 11-15-2000 at 03:45 PM]
Actually, you should set the KeyAscii to 0 rather than 13.
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
duh..I knew that...it's those cheap drugs....ran out.LOL..:D
larryn, instead of calling text2.setfocus, you should use SendKeys "{TAB}". That way, you won't have to keep updating the code if you change text2's name or tab order.