• Restrict Keypress to allow numbers and backspace

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Restrict Keypress to allow numbers and backspace

    Author
    Topic
    #461762

    I would like to add the allow backspace in addition to 0-9 integers below. I believe the Ascii code is 13 but i can’t get it to work with the following.

    Private Sub txtSSN_KeyPress(KeyAscii As Integer)
    If Chr(KeyAscii) 9 or Chr(KeyAscii)=13 Then
    txtSSN.Text = “”
    KeyAscii = 0
    MsgBox “You can’t enter letters in this field.”, , “Health Quest Visit Arhive”
    End If
    End Sub

    Help! Thank you.

    Viewing 1 reply thread
    Author
    Replies
    • #1172792

      I presume you are trapping keystrokes as they are entered, in which case the ASCII code for Backspace (BS) is decimal 8, while the numbers run from 48 for 0 to 57 for 9. But that begs the bigger question of why you are trapping keystrokes. It appears your user is supposed to enter a SSN as a text string, so you should also allow the arrow keys and the delete key, as users can get creative in how they fix errors. So why not let them enter whatever they type, and then check the data on the After Update event for the control. That lets you check for example that they have entered the correct number of characters (9) using the LEN function, and also check for numeric entries using the IsNumeric function. In other words use the built-in functions rather than trying to do low-level error checking – you will reduce the amount of code you have to write.

      • #1172818

        I presume you are trapping keystrokes as they are entered, in which case the ASCII code for Backspace (BS) is decimal 8, while the numbers run from 48 for 0 to 57 for 9. But that begs the bigger question of why you are trapping keystrokes. It appears your user is supposed to enter a SSN as a text string, so you should also allow the arrow keys and the delete key, as users can get creative in how they fix errors. So why not let them enter whatever they type, and then check the data on the After Update event for the control. That lets you check for example that they have entered the correct number of characters (9) using the LEN function, and also check for numeric entries using the IsNumeric function. In other words use the built-in functions rather than trying to do low-level error checking – you will reduce the amount of code you have to write.

        Did you mean the BeforeUpdate event rather than the AfterUpdate event?

        • #1172867

          Did you mean the BeforeUpdate event rather than the AfterUpdate event?

          That would probably be better, but you can do it on either when you are dealing with a control rather than a record. If you do it on the AfterUpdate event then you have to force focus back to the control, while the BeforeUpdate event would leave focus on the control.

    • #1172793

      BackSpace has ASCII code vbKeyBack = 8, not 13 (13 is Return).

      Code:
      Private Sub txtSSN_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
      	Case vbKeyBack, vbKey0 To vbKey9
      	  ' OK, do nothing
      	Case Else
      	  Beep
      	  KeyAscii = 0
        End Select
      End Sub

      I replaced the message box with a beep, because a message box would soon become very annoying, in my opinion.
      And you only need to set KeyAscii to 0 to cancel the keystroke.

      But I’d follow Wendell’s suggestion.

      • #1172802

        I initially thought stopping the user from making the error (trapping) would be preferable. However I like the idea of using the native functionality to simplify things. Thank you for the suggestions!

        BackSpace has ASCII code vbKeyBack = 8, not 13 (13 is Return).

        Code:
        Private Sub txtSSN_KeyPress(KeyAscii As Integer)
          Select Case KeyAscii
        	Case vbKeyBack, vbKey0 To vbKey9
        	  ' OK, do nothing
        	Case Else
        	  Beep
        	  KeyAscii = 0
          End Select
        End Sub

        I replaced the message box with a beep, because a message box would soon become very annoying, in my opinion.
        And you only need to set KeyAscii to 0 to cancel the keystroke.

        But I’d follow Wendell’s suggestion.

    Viewing 1 reply thread
    Reply To: Restrict Keypress to allow numbers and backspace

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: