Hi again June7. Thanks for the feedback. I have successfully used dynamic parameters in a query where clause many times. I have heeded your advice though and am back to using Allen Browne's code for multiple criteria and am getting an error 3075 Extra ) in query expression '([status]="Pending") AND ([bidder]="") And ([biddate]=)' and when I open the form in design view after clearing the filter, the previous filter is still there ([status] = "") AND ([bidder] = "Sam Anderson") AND ([biddate] = ).
Code:
On Error Resume Next
On Error GoTo errHandler
'Purpose: This module illustrates how to create a search form, _
where the user can enter as many or few criteria as they wish, _
and results are shown one per line.
'Note: Only records matching ALL of the criteria are returned.
'Author: AlLeng Browne (alLeng@alLengbrowne.com), June 2006.
'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
we remove the trailing " AND " at the end.
' 2. The date range works like this: _
Both dates = only dates between (both inclusive. _
Start date only = all dates from this one onwards; _
End date only = all dates up to (and including this one).
Dim strWhere As String 'The criteria string.
Dim lngLeng As Long 'Lenggth of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
'***********************************************************************
'Look at each search box, and build up the criteria string from the non-blank ones.
'***********************************************************************
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.statusfilter) Then
strWhere = strWhere & "([status] = """ & Me.statusfilter & """) AND "
End If
If Not IsNull(Me.bidderfilter) Then
strWhere = strWhere & "([bidder] = """ & Me.bidderfilter & """) AND "
End If
If Not IsNull(Me.biddatefilter) Then
strWhere = strWhere & "([biddate] = " & Format(Me.biddatefilter, conJetDate) & ") AND "
End If
'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLeng = Len(strWhere) - 5
If lngLeng <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLeng)
'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.filter = strWhere
Me.FilterOn = True
End If
Debug.Print strWhere
Exit Sub
errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
End Sub
And then
Code:
debug.Print strwhere
([status] = "Needs Bid") AND ([bidder] = "") AND ([biddate] = )