Hi Scott,
Here's two control-level data validation routines that will force numeric-only input into a textbox. Non-numeric characters simply can't be entered into the control:
A textbox control's KeyPress event takes one argument, KeyAscii, that returns the ACSII value of the key pressed.
Private Sub Text0_KeyPress(KeyAscii As Integer)
' If a backspace (ASCII-8) or a tab (ASCII-9) was
' entered, allow it unconditionally.
If (KeyAscii = 8) Or (KeyAscii = 9) Then Exit Sub
' Chr$ pulls character value from ASCII value, which
' is then evaluated as a number. If value is not numeric,
' KeyAscii value set to 0, which prevents any text from
' being entered.
If Not IsNumeric(Chr$(KeyAscii)) Then KeyAscii = 0
End Sub
Private Sub Text0_KeyPress(KeyAscii As Integer)
'If a backspace (ASCII-8) or a tab (ASCII-9) was
' entered, allow it unconditionally.
If (KeyAscii = 8) Or (KeyAscii = 9) Then Exit Sub
' Block the keypress if it is not a digit by comparing the
' KeyAscii value with the ASCII values of the numeric
' characters (ASCII - 48 through 57).
If (KeyAscii < 48) Or (KeyAscii > 57) Then KeyAscii = 0
End Sub
HTH,
Bob
|