Question : Select Query from Form Data

Hi all

I need to show records from a table via a query that gets it's parameters from 4 combo boxes on a form. The question is how do I get the query to ignore a combo box field when the combo is empty or Null.

As the table does not contain empty or null fields unless all 4 boxes have criteria or data, naturaly all I will get is no records. So if I only want to use criteria from the first or last box I need the query to ignore the blank entries in the other 3 combos

Answer : Select Query from Form Data

You can construct a query base like this:

sSql = SELECT myFields FROM myTables WHERE 1=1

And now, use variables for the rest of the parameters:

if (Not isNull(combo1.Value)) then
  sConditions = sConditions & " AND fieldCombo1 = " & combo1.Value
end if

if (Not isNull(combo2.Value)) then
  sConditions = sConditions & " AND fieldCombo2 = " & combo2.Value
End if

Finally you only add your conditions to your original query
sSql = sSql & sConditions

And execute this sSql query.
Random Solutions  
 
programming4us programming4us