|
-
Nov 22nd, 2000, 04:01 PM
#1
Thread Starter
Addicted Member
I know i have seen this before on the board, but i can't seem to find it. I want the user to be able to only enter numbers, besides backspace and spacebar. Thank you
-
Nov 22nd, 2000, 04:08 PM
#2
Frenzied Member
Even though I am againts relying on Errors:
Code:
Text1_Change()
Dim tester As Integer
On Error GoTo notnumber
tester = CInt(Text1.Text)
notnumber:
MsgBox 'whatever
End Sub
Im not even sure if that will work, but its worth a try.
retired member. Thanks for everything 
-
Nov 22nd, 2000, 04:19 PM
#3
Hyperactive Member
The easiest (and in some respects the best) approach is to use the Validate event:
Code:
Sub Text1_Validate(Cancel As Boolean)
Me.Text1 = Val(Me.Text1)
End Sub
The other way is through the KeyPress event:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13 ' Enter
Case 32 ' Space
Case Is < 48 ' Under 0
Case Is > 57 ' Over 9
Case Else
KeyAscii = 0
End Select
End Sub
The thing you want to remember is that some characters are not digits, but they are numeric. Period, plus, minus, e, and dollar sign (if you use the IsNumeric function, $ is valid) are all possible additions to the above cases.
-
Nov 22nd, 2000, 04:20 PM
#4
Thread Starter
Addicted Member
I was leaning more towards not even allowing the user to enter them, where no msg box is needed
-
Nov 22nd, 2000, 04:28 PM
#5
Hyperactive Member
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (IsNumeric(Chr(KeyAscii))) And KeyAscii > 32 Then
KeyAscii = 0
End If
End Sub
-
Nov 22nd, 2000, 04:35 PM
#6
Thread Starter
Addicted Member
Thank you, I found marnitzg to work best.
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
|