Answered by:
Alphanumeric validation for textbox | No numbers or characters allowed.

Question
-
Hello everyone,
How can I program a text box so that no number or characters are accepted, only back pace and the tab key.
Thanks everyone!
Syn
Friday, September 30, 2011 3:03 AM
Answers
-
Hi Synthologic,
You might try this, place this Function into a seperate Module and save it, for example: basCharsOnly.
Public Function ChkOnlyChars(KeyAscii As Integer) Dim ctrl As Control Set ctrl = Screen.ActiveControl Select Case KeyAscii Case Asc("a") To Asc("z") Case Asc("A") To Asc("Z") Case Asc("-") If InStr(1, ctrl.value, "-") > 0 Or ctrl.SelStart > 0 Then KeyAscii = 0 End If Case Asc(".") If InStr(1, ctrl.value, ".") > 0 Then KeyAscii = 0 End If Case Else KeyAscii = 0 End Select End Function
_________________________
Then in the KeyPress event of your Textbox, place the following line:
Private Sub txtYourTextFieldNameHere_KeyPress(KeyAscii As Integer) ChkOnlyChars KeyAscii End Sub
Hope this helps,
Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"Tuesday, January 31, 2012 4:24 AM
All replies
-
Hi,
When you press and release a key while a control on a form has the focus (or use the SendKeys action or statement to send a keystroke), the following sequence of events occurs:
KeyDown → KeyPress → KeyUp
If pressing a key triggers another event for a control, the other event occurs after the KeyPress event but before the KeyUp event. For example, if a keystroke changes text in a text box, triggering a Change event, the following sequence of events occurs:
KeyDown → KeyPress → Change → KeyUp
Use these events to determine if an allowed key is pressed. All you need to do is check if a backspace or tab key is pressed, don't check for all keys that are not allowed only keys that are allowed.
Code like this as an example
Private Sub Textbox_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode <> vbKeyTab Or KeyCode <> vbKeyBack) Then
KeyCode = 0 'ignore the key
End If
End Sub
"The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred BrooksFriday, September 30, 2011 8:28 AM -
Hi,
When you press and release a key while a control on a form has the focus (or use the SendKeys action or statement to send a keystroke), the following sequence of events occurs:
KeyDown → KeyPress → KeyUp
If pressing a key triggers another event for a control, the other event occurs after the KeyPress event but before the KeyUp event. For example, if a keystroke changes text in a text box, triggering a Change event, the following sequence of events occurs:
KeyDown → KeyPress → Change → KeyUp
Use these events to determine if an allowed key is pressed. All you need to do is check if a backspace or tab key is pressed, don't check for all keys that are not allowed only keys that are allowed.
Code like this as an example
Private Sub Textbox_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode <> vbKeyTab Or KeyCode <> vbKeyBack) Then
KeyCode = 0 'ignore the key
End If
End Sub
"The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
Thanks againFriday, September 30, 2011 12:27 PM -
Hi Synthologic,
You might try this, place this Function into a seperate Module and save it, for example: basCharsOnly.
Public Function ChkOnlyChars(KeyAscii As Integer) Dim ctrl As Control Set ctrl = Screen.ActiveControl Select Case KeyAscii Case Asc("a") To Asc("z") Case Asc("A") To Asc("Z") Case Asc("-") If InStr(1, ctrl.value, "-") > 0 Or ctrl.SelStart > 0 Then KeyAscii = 0 End If Case Asc(".") If InStr(1, ctrl.value, ".") > 0 Then KeyAscii = 0 End If Case Else KeyAscii = 0 End Select End Function
_________________________
Then in the KeyPress event of your Textbox, place the following line:
Private Sub txtYourTextFieldNameHere_KeyPress(KeyAscii As Integer) ChkOnlyChars KeyAscii End Sub
Hope this helps,
Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"Tuesday, January 31, 2012 4:24 AM