-
This is driving me crazy, i learned vb 2 years ago, just now getting back into it, i can't remeber how i did this??
I'm sure it's simple, but, if i have a text box, and want the user to only be able to enter numbers, how can i implement that. I can figure out how with using a message box. If the user tries to enter a letter in, it just wont let them. thanks
-
here you go:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 13
MsgBox "Numbers only"
End Sub
-
..
thanks for the reply, i figured out how to do that, but i dont want a msg box to pop up, just not to allow the user to enter letters. thanks
-
here:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 13
End Sub
-
That will not allow backspace though dimava. Just a little addition...
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 13
End If
End Sub
By the way, is it:
KeyAscii = 13
or
KeyAscii = 0
???
[Edited by Matthew Gates on 11-03-2000 at 11:29 PM]
-
-
You are right.
0 = Nothing
13 = Enter
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
-
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Another variation on the theme
Select Case KeyAscii
'Allow Backspace, 0 - 9 only
Case 8, 48 To 57
Case Else
Beep
KeyAscii = 0
End Select
Cheers
Adrian.
-
I always did it this way...
Code:
Private Sub txtStuff_keypress(KeyAscii as Integer)
If not isnumeric (Chr(KeyAscii)) and KeyAscii<>8 and KeyAscii<>46 then
Beep
KeyAscii = 0
End Sub
That allows for backspaces and decimal points...
[Edited by CyberSurfer on 11-04-2000 at 10:04 AM]
-
The code I provided takes care of all keys like backspace, space, etc. etc. which you may need.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
'^let's all these good/important keys pass
If Not IsNumeric(Chr(KeyAscii)) Then
'^alphabet/!@#$%^&*()_+ = ignored
KeyAscii = 0
'If the key pressed is not allowed, than don't allow
'it to go through to the textbox
End If
End Sub
Short, Simple, and Sweet :rolleyes:.