Click to See Complete Forum and Search --> : Numeric Code in Textbox?
Smie
Jan 3rd, 2000, 05:37 AM
Could someone please tell me how to make a textbox numeric only? This would help me greatly, thanks!
Gimpster
Jan 3rd, 2000, 06:01 AM
It's pretty nasty, but you could just an if then statement in the Text1_KeyPress() event. Something like:
If KeyAscii <> vbKey0 or...KeyAscii <> vbKey9 Then
x = Len(Text1.Text)
x = x - 1
Text1.Text = Mid(Text1.Text, 1, x)
End If
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.
------------------
Ryan
[This message has been edited by Gimpster (edited 01-03-2000).]
jpark
Jan 3rd, 2000, 06:23 AM
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
mhayenga
Jan 3rd, 2000, 06:47 AM
Here's the code that my VB book taught me... It seems simpler than the ones mentioned earlier.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > Asc("9") Then KeyAscii = 0
End Sub
Hope that helps.
Mitchell Hayenga
mitch@hayenga.com
Ruchi
Jan 3rd, 2000, 07:14 AM
Another way....
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
Ruchi
jpark
Jan 3rd, 2000, 07:19 AM
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).]
Ruchi
Jan 3rd, 2000, 07:35 AM
Okay, if you want to use BackSpace key, then try this one ...
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Ruchi :)
Smie
Jan 3rd, 2000, 07:39 AM
Thank you!
mhayenga
Jan 3rd, 2000, 09:33 AM
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
mitch@hayenga.com
[This message has been edited by mhayenga (edited 01-03-2000).]
silla sanjeev kumar
Jan 3rd, 2000, 10:53 PM
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
------------------
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.