locked
Alphanumeric validation for textbox | No numbers or characters allowed. RRS feed

  • 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"
    • Proposed as answer by danishani Tuesday, January 31, 2012 4:24 AM
    • Marked as answer by danishani Tuesday, February 7, 2012 7:56 PM
    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:

    KeyDownKeyPressKeyUp

    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:

    KeyDownKeyPressChangeKeyUp

    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
    Friday, 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:

    KeyDownKeyPressKeyUp

    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:

    KeyDownKeyPressChangeKeyUp

    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
    Sorry, I noticed all the questions did not get included in the post. This is what I am trying to do. I need the text box to accept only letters, no numbers numbers or symbol. However, I was also meaning that the user should be able to tab in the text box or use the backspace key, but not numbers or symbols.

    Thanks again
    Friday, 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"
    • Proposed as answer by danishani Tuesday, January 31, 2012 4:24 AM
    • Marked as answer by danishani Tuesday, February 7, 2012 7:56 PM
    Tuesday, January 31, 2012 4:24 AM