The basic sql string should read:
'Select fields From table Where Condition' or
'Select fields From table Where Condition And Condition' or
'Select fields From table Where Condition And Condition And Condition' etc
The sql string concatenation begins each Condition with ' And ' because it does not know which condition is the first it will encounter based on arbitrary user selections. The mid$() strips out the ' And ' for the first condition so the SQL does not read:
'Select fields From table Where AND Condition' or
'Select fields From table Where AND Condition And Condition' or
'Select fields From table Where AND Condition And Condition And Condition' etc
To see this in operation modify the end of the code a bit:
If strWHERE <> "" Then strSQL = strSQL & "where " & strWHERE
MsgBox strWHERE
BuildSQLString = False
and you'll see the starting ' And ' in the Where portion of the SQL clause that will cause it to choke that the Mid$() function would otherwise strip out.
The best reference on SQL is the QBE query designer. Build your query using the visual designer tools and then choose 'SQL' from the view menu. It's free and it's always there for basic queries (not crosstabs and union queries though, but it's a start).
When learning to write SQL strings, it is often helpful to Debug.Print the string to the Immediate window whence you can copy/paste it into the SQL view of a new query. The QBE Designer will highlight SQL syntax errors that you won't otherwise get notification of.
Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]