View Full Version : [SOLVED:] Message Box if TB Contains Certian Characters
stapuff
01-13-2005, 11:12 AM
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
Zack Barresse
01-13-2005, 11:20 AM
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?
stapuff
01-13-2005, 11:34 AM
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
Zack Barresse
01-13-2005, 11:35 AM
Anytime Kurt! Great to see you round!
Hope the new year is treating you right. :)
Steiner
01-13-2005, 11:21 PM
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.