
Originally Posted by
VBFnewcomer
set the keypreview property of form as
True
see if u like the shorter code
vb Code:
If KeyAscii = 8 Then Exit Sub
If InStr("1234567890", Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If

Originally Posted by
VBFnewcomer
forget you textbooks. If you want real coding check with our members. Incidentally if you are wondering how to use it -
InStr(here type the chars you want to allow even space, Chr(KeyAscii)) = 0 Then
you'll love it in long run
The above code works but not a good way.
Try below:
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'-- allow only digits input
Select Case Chr(KeyAscii)
Case "0" To "9", vbBack
'do nothing
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'-- allow only letters A-Z, also convert a-z to uppercase
Select Case KeyAscii
Case Asc("A") To Asc("Z"), vbBack
'do nothing
Case Asc("a") To Asc("z")
KeyAscii = Asc(UCase(Chr(KeyAscii)))
Case Else
KeyAscii = 0
End Select
End Sub