Multiple Criteria Search Filter on Query (1 Viewer)

Ceebee86

Registered User.
Local time
Today, 09:18
Joined
Sep 9, 2019
Messages
25
Hi Access world,

I am using http://allenbrowne.com/ser-62.html tutorial to make a Filter for a form that's record source is a query.

I have used the tutorial very well, up until the point I tried to do a Date search between two dates.

Code:

Code:
Private Sub cmdFilter_Click()
 '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 lngLen As Long                      'Length 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.
    '***********************************************************************


    If Not IsNull(Me.ComboCompany) Then
        strWhere = strWhere & "([COMPANYID] = " & Me.ComboCompany & ") AND "
    End If
    
    'Text field example. Use quotes around the value in the string.
    If Not IsNull(Me.ComboSupplier) Then
        strWhere = strWhere & "([aglgrnSUPPLIER] = """ & Me.ComboSupplier & """) AND "
    End If
    
    'Text field example. Use quotes around the value in the string.
    If Not IsNull(Me.ComboProduct) Then
        strWhere = strWhere & "([ADVISEDPRODUCT] = """ & Me.ComboProduct & """) AND "
    End If
    
    'Date field example. Use the format string to add the # delimiters and get the right international format.
    If Not IsNull(Me.TextDeliveryDateSearch) Then
        strWhere = strWhere & "([SCANINTIME] >= " & Format(Me.TextDeliveryDateSearch, conJetDate) & ") AND "
    End If
    
    If Not IsNull(Me.TextDeliveryDateSearchToo) Then   'Less than the next day.
        strWhere = strWhere & "([SCANINTIME2] <= " & Format(Me.TextDeliveryDateSearchToo, conJetDate) & ") AND "
    End If

    If (Me.FrameDeliveryPreAdvice = 3) Then
        strWhere = strWhere
    End If
        
    If (Me.FrameDeliveryPreAdvice = 2) Then
    
    strWhere = strWhere & "([DeliveryType] = 2) AND "
    End If
    
    If (Me.FrameDeliveryPreAdvice = 1) Then
    
    strWhere = strWhere & "([DeliveryType] = 1) 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.
    lngLen = Len(strWhere) - 5
    If lngLen <= 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, lngLen)
        'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
        'Debug.Print strWhere
Debug.Print strWhere

        'Finally, apply the string as the form's Filter.
        Me.subfrmWarehouse.Form.Filter = strWhere
        Me.subfrmWarehouse.Form.FilterOn = True
    End If
End Sub


SCANINTIME & SCANINTIME2 are the same field, I thought that making a new field in the query would help, but it didn't really. It is stored in a table and is inputted to the table by =Now() and Format was 'Short Date'....I originally thought this was the issue, so I manually amended all date from '11/01/2019 15:01:44' to '11/01/2019' for example. No effect.

I think the issue is with my data, but I've nearly given up, and I am hoping you guys/girls can help me narrow it down.
 

Ceebee86

Registered User.
Local time
Today, 09:18
Joined
Sep 9, 2019
Messages
25
Forgot to mention, if I search 30/10/2019 - 01/11/2019.

I get no results.

If I search within the months, and not across them, I get results. Frustrating. :(
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:18
Joined
Oct 29, 2018
Messages
21,467
Hi. When you enter the date criteria, what does the Debug.Print strWhere show you?
 

Ceebee86

Registered User.
Local time
Today, 09:18
Joined
Sep 9, 2019
Messages
25
Hi. When you enter the date criteria, what does the Debug.Print strWhere show you?

When I use criteria 01/11/2019 - 13/11/2019

StrWhere = ([SCANINTIME] >= #11/01/2019#) AND ([SCANINTIME] <= #11/13/2019#)

I get results.

If I do 30/10/2019 - 06/11/2019

StrWhere = ([SCANINTIME] >= #11/01/2019#) AND ([SCANINTIME] <= #11/13/2019#)

I do not get any results, blank page.

And...since changed SCANINTIME2 back to SCANINTIME.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:18
Joined
Oct 29, 2018
Messages
21,467
When I use criteria 01/11/2019 - 13/11/2019

StrWhere = ([SCANINTIME] >= #11/01/2019#) AND ([SCANINTIME] <= #11/13/2019#)

I get results.

If I do 30/10/2019 - 06/11/2019

StrWhere = ([SCANINTIME] >= #11/01/2019#) AND ([SCANINTIME] <= #11/13/2019#)

I do not get any results, blank page.

And...since changed SCANINTIME2 back to SCANINTIME.
Hi. Thanks! There must be a typo in your second Debug.Print result. Did you happen to copy and paste the same result from the first set of criteria? In any case, I suggest try adding the CDate() function to your code to see if it helps. For example:
Code:
Format([B]CDate(Me.TextDeliveryDateSearch)[/B], conJetDate)
 

Ceebee86

Registered User.
Local time
Today, 09:18
Joined
Sep 9, 2019
Messages
25
Hi. Thanks! There must be a typo in your second Debug.Print result. Did you happen to copy and paste the same result from the first set of criteria? In any case, I suggest try adding the CDate() function to your code to see if it helps. For example:
Code:
Format([B]CDate(Me.TextDeliveryDateSearch)[/B], conJetDate)

Thanks, I'll give this a go tonight. :)
 

Users who are viewing this thread

Top Bottom