Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate and filter incorrect parameters.

Status
Not open for further replies.

djmc

Programmer
Jun 12, 2002
179
CA
I have a form which lets the user search for things. Depending on a combo box, the parameter could be a text or number. The search function will generate the querie corresponding to the combo box/parameter.

I would like to know what is the conventional way of filtering a textbox before it gets appeneded onto an SQL string and executed. I.e if some user types in "[blahlhala]" or some characters like "]\}.?&$" etc I would like to filter them so that it will not mess up the query or validate it ebefore it gets executed. In general I would like to filter

1. a parameter that is all letters in range A-Z and a-z is valid
2. a parameter that is a number is valid

I would like to know how to check for things like this.
TIA.
 
Lets say your variable for the sting is SearchString

Function Test()

Dim SearchString As String
Dim Cnt
Dim ValChar

SearchString = "Te/t"

Cnt = 1

Do While Cnt <= Len(SearchString)
ValChar = Mid(SearchString, Cnt, 1)
If IsNumeric(ValChar) Then
MsgBox (&quot;Good Charater&quot;)
ElseIf ValChar >= &quot;A&quot; And ValChar <= &quot;Z&quot; Then
MsgBox (&quot;Good Charater&quot;)
Else
MsgBox (&quot;Bad Charater&quot;)
End If
Cnt = Cnt + 1
Loop

End Function
 
Let's say your user entry control is named txtFind.

You could use code something like this. It first checks to see if there is data in the control txtFind. If there is it looks for a value not equal to 0 and assumes that a numeric value is entered, otherwise it looks for a text value.

If IsNull(me.txtFind) then
msgbox &quot;No parameter entered!&quot;
Exit Sub
Else
If val(me.txtFind) <> 0 then
'put your code for the numeric search here
End If
If me.txtFind Between &quot;A&quot; and &quot;ZZZZZZZZZZZZZ&quot; then
'put your code for the text search here
End If
End If

You might change the second test to read

If mid(me.txtFind,1,1) Between &quot;A&quot; and &quot;Z&quot; then

Access ignores the case of text unless specifically told to check the case. If 0 is a valid numeric entry, then the first statement should read:

If val(me.txtFind) <> 0 or me.txtFind = &quot;0&quot; then

(note that this is a zero, not a capital O)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top