Consulting

Results 1 to 7 of 7

Thread: Textbox data validation

  1. #1
    VBAX Regular
    Joined
    Jul 2013
    Posts
    50
    Location

    Textbox data validation

    I usually have to check Textbox against number or date wich is quite easy in VBA, but now I am faced with something I can't figure out letters and numbers

    I have a textbox wich should contain data in the format LL0000, so 2 letters and 4 numbers but I am not quite sure how to validate it using VBA.
    I'll have to use:

    If InStr("/\? * -  # + [ ]", Chr(KeyAscii)) Then KeyAscii = 0
    to prevent symbol from being entered and already limited the number of character to 6, that much I know

    Any help or pointers will help thank you!

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Try this out
    Attached Files Attached Files

  3. #3
    VBAX Regular
    Joined
    Jul 2013
    Posts
    50
    Location
    ok the link you sent me does prevent the special character from being put in the textbox but it doesn't do the validtion I need, I need to check that what is in the checkbox is something like LL0000 or PJ01234. it must not accept something like 123456 or ABCDEF. I need something like mask in access where I can check each character to see if it's a number or a letter.

  4. #4
    VBAX Regular
    Joined
    Jul 2013
    Posts
    50
    Location
    ok I think I figured it out, at least for the numeric part:

    For j = 3 To 6
        If Not IsNumeric(Mid(Me.TxtProjet.Value, i, 1)) Then
            MsgBox ("wrong input"), vbExclamation
            Cancel = True
            Exit Sub
        End If
    Next j
    I'll let you know if it works

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    maybe something like this

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With CreateObject("VBScript.RegExp")
            .Pattern = "[A-Z]{2}[0-9]{4}"
             If Not .test(TextBox1.Value) Then
                MsgBox "Invalid input. Please enter text in ""AB1234"" (two capital letters and 4 numbers) format!"
                Cancel = True
                TextBox1.Value = ""
                TextBox1.SetFocus
            End If
        End With
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Try this
    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
        Cancel = Not (TextBox1.Text Like "[A-Za-z][A-Za-z]####")
        If Cancel Then MsgBox "Please enter two letters followed by four numbers"
    End Sub

  7. #7
    VBAX Regular
    Joined
    Jul 2013
    Posts
    50
    Location

    Thumbs up Solved!

    Tried both mikerickson and mancubus solutions and they both work great!

    I went with
    mikerickson's solution as it is a bit shorter and file size is an issue for what I am working on

    Thank you very much for the answers and sorry for the late reply!
    Last edited by Jomathr; 11-06-2013 at 09:10 AM. Reason: found the solved button

Posting Permissions

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