Restrict characters in Text Box

Posted on 2006-05-17
Last Modified: 2008-01-09
I have text boxes that restrict the characters that are entered. The code is below:

If Me.TextBoxName.Value Like "*[!A-Z]*" Then
        Cancel = True
End If

I need it to allow hyphens also...

Question by:Sariff
    LVL 65

    Accepted Solution

    why not put a and in it?

    Im assuming this is on the keypress function? u can do validation per keypress then

    If Me.TextBoxName.Value Like "*[!A-Z]*" and  Me.TextBoxName.Value != "-" Then
            Cancel = True
    End If

    LVL 15

    Assisted Solution

    Add the function listed below, then in the Keypress event of the textbox use it like this:

    Private Sub txtNumber_KeyPress(KeyAscii As Integer)
    End Sub

    Replace the characters in quotes with the ones you wish to allow

    Public Function ValidateEntry(ValidKeys As String, KeyAscii As Integer) As Integer
    'Pass a list of acceptable keys, and the ascii code of the key being pressed
    'it will return the ascii code if the key is in the list, or 0 if not
    'call it from the On_keypress event of a text box eg the following example will
    'restrict key entry to valid numerics plus decimal point

    'Private Sub txtNumber_KeyPress(KeyAscii As Integer)
    'End Sub

    On Error GoTo HandleErr

    If InStr(ValidKeys, Chr$(KeyAscii)) > 0 Or KeyAscii = 8 Then  'KeyAscii=8 allows the backspace
        ValidateEntry = KeyAscii
        ValidateEntry = 0
    End If

      Exit Function

      Select Case Err.Number
        Case Else
           MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "modFunctions.ValidateEntry"
      End Select
      Resume ExitHere
    ' End Error handling block.
    End Function

    Author Comment

    Actually I am using the code in the 'Before Update" event. It works well. The hyphen part does not work...

    Author Comment

    I found my answer. Thanks!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now