I want a text box to only accept letters & numbers. I dont want to use a masked edit box as this limits the length of the text to be entered. I assume I have to use Win32 api setwindowlong, etc. Can anyone help?
Printable View
I want a text box to only accept letters & numbers. I dont want to use a masked edit box as this limits the length of the text to be entered. I assume I have to use Win32 api setwindowlong, etc. Can anyone help?
in the ELSE where I set KeyAscii = 0 you could put a message box telling them they entered an invalid character.Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then
'this means it's a number
ElseIf KeyAscii >= 65 And KeyAscii <= 90 Then
'this means it's a capital letter
ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then
'this means it's a lower case letter
Else
KeyAscii = 0 'it won't write anything to the text box.
'this means it's a different character
End If
End Sub
Hello DHorton,
Hopefully this will help you.
Private Sub Text1_KeyPress(KeyAscii As Integer)
'character Ascii
'0 - 9 48 - 57
'A - Z 65 - 90
'a - z 97 - 122
If Not (KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii >= 65 And KeyAscii <= 90 Or KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = 0
End Sub
Nice regards,
Michelle.
or...
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Is <= 32, 48 To 57, 65 To 90, 97 To 122
KeyAscii = KeyAscii
Case Else
KeyAscii = 0
End Select
End Sub
the is <= 32 is to let the user use the control keys
backspace, delete, etc....