How Do I go about setting up restrictions on what can be typed into a text box? I need it set up so that the spacebar can't be used, as well as ascii symbols. (Basically I only want my users to be able to use letters and numbers)
Thanks
Adrift
Printable View
How Do I go about setting up restrictions on what can be typed into a text box? I need it set up so that the spacebar can't be used, as well as ascii symbols. (Basically I only want my users to be able to use letters and numbers)
Thanks
Adrift
You can restrict characters by using the KeyPress event:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strUserChar As String
' Allow "control characters" like backspace and
' and arrow keys ...
If KeyAscii < 32 Then Exit Sub
' If the character is anything other than a letter
' or number, set the KeyAscii argument to 0, which
' converts the user's keypress to "null" ...
strUserChar = Chr$(KeyAscii)
If (strUserChar >= "0" And strUserChar <= "9") _
Or (strUserChar >= "A" And strUserChar <= "Z") _
Or (strUserChar >= "a" And strUserChar <= "z") Then
' it's OK, do nothing
Else
KeyAscii = 0
End If
End Sub