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
Printable View
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
Even though I am againts relying on Errors:
Im not even sure if that will work, but its worth a try.Code:Text1_Change()
Dim tester As Integer
On Error GoTo notnumber
tester = CInt(Text1.Text)
notnumber:
MsgBox 'whatever
End Sub
The easiest (and in some respects the best) approach is to use the Validate event:
The other way is through the KeyPress event:Code:Sub Text1_Validate(Cancel As Boolean)
Me.Text1 = Val(Me.Text1)
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.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
I was leaning more towards not even allowing the user to enter them, where no msg box is needed
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (IsNumeric(Chr(KeyAscii))) And KeyAscii > 32 Then
KeyAscii = 0
End If
End Sub
Thank you, I found marnitzg to work best.