|
-
Nov 3rd, 2000, 10:59 PM
#1
Thread Starter
PowerPoster
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
-
Nov 3rd, 2000, 11:09 PM
#2
Frenzied Member
here you go:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 13
MsgBox "Numbers only"
End Sub
NXSupport - Your one-stop source for computer help
-
Nov 3rd, 2000, 11:12 PM
#3
Thread Starter
PowerPoster
..
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
-
Nov 3rd, 2000, 11:16 PM
#4
Frenzied Member
here:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 13
End Sub
NXSupport - Your one-stop source for computer help
-
Nov 3rd, 2000, 11:26 PM
#5
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]
-
Nov 4th, 2000, 12:18 AM
#6
Junior Member
-
Nov 4th, 2000, 01:03 AM
#7
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
-
Nov 4th, 2000, 09:38 AM
#8
Lively Member
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.
-
Nov 4th, 2000, 09:59 AM
#9
Hyperactive Member
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]
-
Nov 4th, 2000, 12:38 PM
#10
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 .
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
|