| Answer: |
We will assume that the first page is a form something like this:
<FORM Action="page2.asp" Method=Post> Company name: <INPUT Name="Company" Size=60> City name: <INPUT Name="City" Size=60> Type of product: <SELECT Name="Product"> <OPTION Value="">-- you may choose one -- <OPTION>Food <OPTION>Fuel <OPTION>Hardware <OPTION>Software </SELECT> ... </FORM>
|
Note that we purposely show a mixture of text and SELECT specifications that the user can make.
So now, on the next page (Page2.asp), we can do this:
<% ' put the list of searchable fields into an array, thus: formFields = Array("Company", "City", "Product") ' and the list of TABLE field names into a parallel array, thus: dbFields = Array("CompanyName", "CityName", "ProductType")
where = "" ' we will build the WHERE clause... delimiter = " WHERE "
For fnum = 0 To UBound( formFields ) fval = Request.Form( formFields(fnum) ) If ("X" & fval) <> "X" Then ' user gave us a value for this field! ' ...build one LIKE clause, for this field... ' (of course, this could be an = test instead of LIKE) where = where & delimiter & dbFields(fnum) & " LIKE '%" & fval & "%'" ' change delimiter to the conjunction now! delimiter = " OR " ' this might be " AND " instead! End If Next ' the WHERE clause is built...or is it? If Len(where) = 0 Then ' ??? the user did not give *any* values ??? ' ??? YOU must decide what to do ??? End If ' so build the full query: SQL = "SELECT * FROM table " & where %>
|
Do you see that? By putting the field names into arrays, the whole thing is wondefully flexible. Add more fields or remove some at any time by just changing the array contents. No need to change any actual logic in the code.
NOTE: If the DB fields and the form fields have the same name, then of course you can use only one array. We show it with a pair of arrays to emphasize the flexibility.
|