anybody have CHARACTER?

  • Thread starter Thread starter dkuntz
  • Start date Start date
D

dkuntz

Guest
How do I prevent a textbox from receiving non alpha CHARACTERS?
All I want accepted in this textbox are numbers and letters.

This is in a VB app. does that make a difference?

....of course, any help is always appreciated!

Thanks -

Derek
 
This will get you started. I use the KeyPress event to limit what can be keyed into a text box.

If KeyAscii = Asc("/") Or KeyAscii = Asc("\") Or KeyAscii = Asc("'") Or KeyAscii = Asc(".") _
Or KeyAscii = Asc("@") Or KeyAscii = Asc("#") Or KeyAscii = Asc("$") Or KeyAscii = Asc("\") _
Or KeyAscii = Asc("(") Or KeyAscii = Asc(")") Or KeyAscii = Asc("*") Or KeyAscii = Asc("-") Or KeyAscii = Asc("_") Then
KeyAscii = 0
MsgBox "Invalid characters", vbInformation, "Invalid Entry"
Else

End If

HTH
 
This code is a bit simpler, it places a space whenever a non Numeric or Alphabetic key is hit:

Private Sub Text10_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 97 To 122, 65 To 90, 48 To 57, 32, 8 '8 is to allow for bkspace
KeyAscii = KeyAscii
Case Else
KeyAscii = 32
End Select

End Sub


If you don't want a space then change KeyAscii = 32 to KeyAscii = 0. That will keep anything from being typed.

Peace
 
Last edited:
Thanks to both of you! This is pretty much what I need.

Derek
 
Thanks Drevlin, that is a lot cleaner than my method. I also like to give the users a message that what they did was wrong.

Private Sub Text0_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 97 To 122, 65 To 90, 48 To 57, 8 '8 Backspace '32 Space
KeyAscii = KeyAscii
Case Else
MsgBox "Only alpha or numeric characters are allowed.", vbInformation, "Invalid Key"
'KeyAscii = 32 'key a space
KeyAscii = 0 'key nothing
End Select
End Sub
 

Users who are viewing this thread

Back
Top Bottom