Here is how I would do this:
Code:
dim sql
sql = "SELECT * FROM dbo.qrySearchResults WHERE 1=1 "
if trim(request.form("CountyID")) <> "" then
sql = sql & " AND CountyID = " & trim(request.form("CountyID"))
end if
if trim(request.form("ConTypeID")) <> "" then
sql = sql & " AND ConTypeID = " & trim(request.form("ConTypeID"))
end if
if trim(request.form("ConTypeID")) <> "" then
'Only choose one of these. the second will gve you more resutls using wildcard
'sql = sql & " AND ConBusName LIKE '" & trim(request.form("ConTypeID")) & "' "
sql = sql & " AND ConBusName LIKE '%" & trim(request.form("ConTypeID")) & "%' "
end if
sql = sql & " ORDER BY ConBusName;"
Note:
- I have placed WHERE 1=1 at the beginning of the SQL so all of the SQL inside each conditional statement can safely use AND without worrying about which get fired in what order etc...
- The conditional SQL assumes CountyID and ConTypeID are int datatypes in your DB. It assumes ConBusName is some kind of string
- Your code used this:
String query =
This would have never run, you can have spaces in variable names
- This:
Query = Query +
will not work either. you must use & to concatenate
- No need to use ASC, this is default sql ordering