Consulting

Results 1 to 5 of 5

Thread: Message Box if TB Contains Certian Characters

  1. #1

    Message Box if TB Contains Certian Characters

    On Userform4

    How do I create this IF statement?

    If TextBox1.Value has any of these characters in it ?,/,.,<,],[ then clear Textbox1.value to ""
    and message box appears informing that an unexceptable name has been entered into the textbox.

    I have the back half of the IF statement.

    Thanks,

    Kurt

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hey Kurt!


    You could use a loop/iteration coupled with a Select Case statement like this ...


    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim i As Long, hasBadVal As Boolean
        hasBadVal = False
        For i = 1 To Len(Me.TextBox1.Value) Step 1
            Select Case Mid(Me.TextBox1.Value, i, 1)
            Case "?", "/", ".", "<", "]", "["
                hasBadVal = True
                Exit For
            End Select
        Next i
        If hasBadVal Then
            MsgBox "An unexceptable name has been entered into the textbox!", _
                vbCritical, "ERROR"
            Me.TextBox1.Value = ""
        Else
            MsgBox "It's up, and it's good!!", vbExclamation, "GOOD"
        End If
    End Sub

    Does that work for you?

  3. #3
    Heya Z

    Works like a champ. Exactly what I needed. I tried several ways to accomplish the task before I posted but based on your code - I was not even close.

    Thanks again for your help & guidance,

    Kurt

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Anytime Kurt! Great to see you round!

    Hope the new year is treating you right.

  5. #5
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Ok, and just for fun, this one should do the same using regular expressions:


    Option Explicit
    Dim RegEx As Object
    Private Sub Command1_Click()
       If RegEx.test(Text1.Text) Then
          MsgBox "Wrong Character"
       Else
          MsgBox "ok"
       End If
    End Sub
    
    Private Sub Form_Load()
       Set RegEx = CreateObject("vbscript.regexp")
       RegEx.Pattern = "[?/.<\][]"
    End Sub


    Daniel

Posting Permissions

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