sept2006 said:
I am using a form to enter criteria values in a query. I would like to make
it so that if I leave a field blank, it will not filter out those values.
How do I do this? I referenced "using parameters with queries and reports"
when designing my form, so hopefully that is something you can look at if you
need reference.
Here's how I do it, but there's probably a better way:
Dim strFilter As String
Dim stsLNAMEFilter As String
Dim strFNAMEFilter As String
Dim strCIDFilter As String
Dim strDateFilter As String
If Len(Trim(Me.LName)) > 0 Then strLastNameFilter = "[LNAME] Like '" &
Me.LName & "*'"
If Len(Trim(Me.FName)) > 0 Then strFirstNameFilter = "[FNAME] = '" &
Me.FName & "'"
If IsNumeric(Me.CID) Then strCIDFilter = "[CID] = '" & Me.CID
If IsDate(Me.StartDate) And IsDate(Me.EndDate) then strDateFilter = "[IDate]
Between #" & Format$(Me.StartDate, "mm/dd/yyyy") & "# AND #" &
Format$(Me.EndDate, "mm/dd/yyyy") & "#"
' now that I have all the filters I connect them with an AND:
If Len(Trim(strLastNameFilter)) > 0 Then strFilter = strLastNameFilter
If Len(Trim(strFirstNameFilter)) > 0 Then strFilter = strFilter & " " &
"AND" & " " & strFirstNameFilter
If Len(Trim(strCIDFilter )) > 0 Then strFilter = strFilter & " " & "AND" &
" " & strCIDFilter
If Len(Trim(strDateFilter )) > 0 Then strFilter = strFilter & " " & "AND" &
" " & strDateFilter
' now I have to get rid of the "AND" if it is the first three characters of
the filter
If Left(strFilter , 3) = "AND" Then
strFilter = Right(strFilter , Len(strFilter ) - 3)
End If
We have our filter with just what we want.