Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jul 2012
    Posts
    13

    Unanswered: How to disable special characters

    I have a database that our users will input part numbers. The numbers sometimes have hyphens or spaces and possibly other special characters. I started a code below but it doens't seem to be working.

    Private Sub PartNumber_Change()
    If Right(Me.PartNumber.Text, 1) Like "[#,!,@,$,%,&, ]" Then Me.PartNumber.Text = Left(Me.PartNumber.Text, Len(Me.PartNumber.Text) - 1)
    End Sub

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    I would use the key press event and test for acceptable characters, set any others to 0. Help should have more info on the event including code.
    Paul

  3. #3
    Join Date
    Aug 2004
    Location
    Cary, NC
    Posts
    264
    This is what I use in one of my applications. Just check the KeyCode and if it's one of the characters you want to exclude set the KeyCode to 0. If you are trying to restrict any of the values on double use keys (like the number keys), you have to check the Shift value, unless you want to exclude both the 5 and the %. If you were just trying to restrict the letter G for example, you wouldn't need to worry about the Shift

    Code:
    Private Sub txtVal_KeyDown(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
        
        'Stop the user from typing [ or ] but still allow { and }
        Case 219, 221
            If (Shift = 0) Then KeyCode = 0
        
        'Stop the user from typing !, @, #, $, % but still allow 1 - 5
        Case 49 To 53
            If (Shift = 1) Then KeyCode = 0
            
        End Select
    
    End Sub

    Steve

  4. #4
    Join Date
    Jul 2012
    Posts
    13
    Paul and Steve,

    Thank you for the help. I am mostly worried about hyphens. Can I just add a "-" to the code?

    Thanks in advance.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    the keyxxx events use the ascii representation of the key. so you need to find the values of the symbols you want and only allow those, or conversely find the values of the symbols you don't want and reject them
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jul 2012
    Posts
    13
    I added the - to the code and it still allows it. How do I get rid of hyphens. I don't care about the !@#$, because they are not typically used in a part number.

    Thanks

    Private Sub Text52_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode

    'Stop the user from typing [ or ] but still allow { and }
    Case 219, 221
    If (Shift = 0) Then KeyCode = 0

    'Stop the user from typing !, @, #, $, %, -, but still allow 1 - 5
    Case 49 To 53
    If (Shift = 1) Then KeyCode = 0

    End Select

    End Sub

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so what processing model do you think you are using
    are you trying to test for the characters you want and exclude all others?
    or
    are you trying to test for the characters you dont' want and accept all others?

    it doesn't matter which way you go about it.
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    You didn't add - to the code, you added it to a comment. You need to find out what the ASCII code is for that character (hint, look up characters in help or add Debug.Print KeyCode and see what it is in the Immediate window) and add it to the test.
    Paul

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Im not convinced that keydown is the right event Keyup may be a better choice
    I'd rather be riding on the Tiger 800 or the Norton

  10. #10
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    I agree, though it apparently works for Steve. I use the key press event.
    Paul

  11. #11
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    For this rather limited task, I'd simply use the Replace() Function:

    Code:
    Private Sub TargetField_AfterUpdate()
     Me.TargetField = Replace(Me.TargetField, "-", "")
    End Sub


    Less confusing, to the user, than pressing a key and having nothing show in response, and still gives the same end result. And who knows, after seeing the program make the correction a few times, the users may realize their mistake!

    Linq ;0)>
    Last edited by Missinglinq; 07-25-12 at 12:56.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  12. #12
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I admire your trust in users

    I think the OP wants to capture only numeric and '{' or '}', and I'm not even certain that its only 1..5.
    thats a heck of a lot of replace statements.
    if the aim is to limit to specific characters then putting code in place in the right key event is the correct way to go in my books
    I'd rather be riding on the Tiger 800 or the Norton

  13. #13
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Quote Originally Posted by healdem View Post

    ...if the aim is to limit to specific characters then putting code in place in the right key event is the correct way to go in my books
    I agree, which is why I normally take that approach, myself, but the OP did say

    Quote Originally Posted by aftermarket_auto View Post

    ...I am mostly worried about hyphens....
    and I did preface my suggestion with "For this rather limited task."

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  14. #14
    Join Date
    Jul 2012
    Posts
    13
    Linq,

    Thank you! That's all I needed. I should have made my original post more specific. Hyphens are used in many part numbers, as well as spaces. Using your code I was able to eliminate that, and not confuse users. 2 birds with 1 stone, thanks again!

  15. #15
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Glad we cold help! But do keep in mind that for a more complicated task, in this same vein, i.e. avoiding the input of a large number of characters, the other suggestions are appropriate.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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