Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Cincinnati, Ohio
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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) < 0 Or 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.

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    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.
    Wendell

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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.

  4. #4
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Cincinnati, Ohio
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

    [quote name='HansV' post='788165' date='09-Aug-2009 16:03']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.[/quote]

  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts
    [quote name='WendellB' post='788164' date='10-Aug-2009 02:52']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.[/quote]
    Did you mean the BeforeUpdate event rather than the AfterUpdate event?

  6. #6
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    [quote name='patt' post='788197' date='09-Aug-2009 17:29']Did you mean the BeforeUpdate event rather than the AfterUpdate event?[/quote]
    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.
    Wendell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •