Could someone please tell me how to make a textbox numeric only? This would help me greatly, thanks!
Printable View
Could someone please tell me how to make a textbox numeric only? This would help me greatly, thanks!
It's pretty nasty, but you could just an if then statement in the Text1_KeyPress() event. Something like:
That way, if they type anything in that isn't a number, then it will delete the last typed character. It's a little sloppy, but it should work just fine.Code:If KeyAscii <> vbKey0 or...KeyAscii <> vbKey9 Then
x = Len(Text1.Text)
x = x - 1
Text1.Text = Mid(Text1.Text, 1, x)
End If
------------------
Ryan
[This message has been edited by Gimpster (edited 01-03-2000).]
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 126) Then
KeyAscii = 0
End If
End Sub
HTH
Joon
Here's the code that my VB book taught me... It seems simpler than the ones mentioned earlier.
Hope that helps.Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > Asc("9") Then KeyAscii = 0
End Sub
Mitchell Hayenga
[email protected]
Another way....
RuchiCode:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
mhayenga,
I don't think it's a great deal, but it still takes some of special characters, which ascii number is less than character "9"
If u want to allow only numbers in the textbox, u have to include the other condition to check for ascii numbers that are less than character "0".
Ruchi,
That works fine, except it doesn't take "BackSpace" key.
Joon
[This message has been edited by jpark (edited 01-03-2000).]
Okay, if you want to use BackSpace key, then try this one ...
Ruchi :)Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Thank you!
Ok, thanks for correcting me earlier... Also, you might want the If-Then statement to allow another ASCII value. The program might require the user to use a decimal point value. I don't know the value of a decimal point off the top of my head, but it should be in the VB help files under "Ascii character codes".
Mitchell Hayenga
[email protected]
[This message has been edited by mhayenga (edited 01-03-2000).]
Hi
it's not diffcult to write code for allowing
numbers in the text box. Most of our friends
replied to u'r question.
try some creative way ..
all the best
with love
sanju
------------------