G
Guest
How can I validate the data in an unbound textbox to not have any special
charactors but allow alpha-numeric?
charactors but allow alpha-numeric?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Klatuu said:You could use an Input Mask, but I don't recommend them. Here is a function
that returns False if the passed string contains no symbols and true if it
does. You can use it in the Before Update event of the textbox and modify it
if there are characters you want to include or exclude:
Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
If BadChars(Me.MyTextBox) Then
MsbBox "Invalid characters entered"
Me.MyTextBox.Undo
Cancel = True
End If
Function BadChars(strSuspect As String) As Boolean
Const conNotGood As String = """!@#$%^&*()_-+={[}]|\:;'?/>.<,`~"
Dim intCtr As Integer
For intCtr = 1 To 32
If InStr(strSuspect, Mid(conNotGood, intCtr, 1)) <> 0 Then
BadChars = True
Exit For
End If
Next intCtr
End Function
TimT said:How can I validate the data in an unbound textbox to not have any special
charactors but allow alpha-numeric?
TimT said:Klatuu,
That is exactly what I was looking to do.
I might have something wrong though, no matter what is entered into the
textbox (which really is an unbound combobox that is not limited to a list),
I get the msgbox.
I discoved the need for this validation when "&" was entered in the text
because the after update event will run a dlookup to check if the discription
exists and if it doesn't it runs a SQL statement to update a table.
Here is the code that I used from your response after I updated it.
Private Sub cmbDescription_BeforeUpdate(Cancel As Integer)
If BadChars(Me.cmbDescription) Then
MsgBox "Descriptions cannot contain special characters" _
& vbCrLf & "Example: !@#$%&*?><", , "MKTTS - No Special Characters"
Me.cmbDescription.Undo
Cancel = True
End If
End Sub
Function BadChars(strSuspect As String) As Boolean
Const conNotGood As String = "!@#$%^*&()_-?><{}][\"
Dim intCtr As Integer
For intCtr = 1 To 32
If InStr(strSuspect, Mid(conNotGood, intCtr, 1)) <> 0 Then
BadChars = True
Exit For
End If
Next intCtr
End Function
Klatuu said:You could use an Input Mask, but I don't recommend them. Here is a function
that returns False if the passed string contains no symbols and true if it
does. You can use it in the Before Update event of the textbox and modify it
if there are characters you want to include or exclude:
Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
If BadChars(Me.MyTextBox) Then
MsbBox "Invalid characters entered"
Me.MyTextBox.Undo
Cancel = True
End If
Function BadChars(strSuspect As String) As Boolean
Const conNotGood As String = """!@#$%^&*()_-+={[}]|\:;'?/>.<,`~"
Dim intCtr As Integer
For intCtr = 1 To 32
If InStr(strSuspect, Mid(conNotGood, intCtr, 1)) <> 0 Then
BadChars = True
Exit For
End If
Next intCtr
End Function
TimT said:How can I validate the data in an unbound textbox to not have any special
charactors but allow alpha-numeric?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.