|
-
Jan 3rd, 2000, 06:37 AM
#1
Thread Starter
Addicted Member
Could someone please tell me how to make a textbox numeric only? This would help me greatly, thanks!
-
Jan 3rd, 2000, 07:01 AM
#2
Hyperactive Member
It's pretty nasty, but you could just an if then statement in the Text1_KeyPress() event. Something like:
Code:
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).]
-
Jan 3rd, 2000, 07:23 AM
#3
Member
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
-
Jan 3rd, 2000, 07:47 AM
#4
Junior Member
Here's the code that my VB book taught me... It seems simpler than the ones mentioned earlier.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > Asc("9") Then KeyAscii = 0
End Sub
Hope that helps.
Mitchell Hayenga
[email protected]
-
Jan 3rd, 2000, 08:14 AM
#5
Member
Another way....
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
Ruchi
-
Jan 3rd, 2000, 08:19 AM
#6
Member
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).]
-
Jan 3rd, 2000, 08:35 AM
#7
Member
Okay, if you want to use BackSpace key, then try this one ...
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Ruchi
-
Jan 3rd, 2000, 08:39 AM
#8
Thread Starter
Addicted Member
-
Jan 3rd, 2000, 10:33 AM
#9
Junior Member
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).]
-
Jan 3rd, 2000, 11:53 PM
#10
Junior Member
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
------------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|