Check String For Invalid Characters

'Add 1 Command Button  and 1 Text Box to your form.
'Insert the string you want to check into the Text Box and press the button.

'Insert this code to your form:

Function IsValidString(StringToTest As String, Invalid As String)
For X = 1 To Len(Invalid)
If InStr(1, StringToTest, Mid$(Invalid, X, 1), 0) Then
IsValidString = False
Exit Function
End If
Next X
IsValidString = True
End Function

Private Sub Command1_Click()
Dim MyString As String
MyString = Text1.Text
'Replace '@#$%^&' with the invalid characters you want to check for.
BadChars$ = "@#$%^&"
X = IsValidString(MyString, BadChars$)
If X Then
MsgBox "This is a valid string"
Else
MsgBox "This is an invalid string"
End If
End Sub

Go Back