Results 1 to 10 of 10
  1. #1
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31

    Need help with code to apply a date filter to a form

    I need help figuring out what's wrong with the code in a filter. I have a form that filters on two fields and also a date filter. Currently the two fields work but I'm having problems incorperating a date filter. Could someone take a look at it and me tell me what's wrong with it? I've attached a copy of the db and the form is called frmSupplierDescriptionCodeqry
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    I don't see any code dealing with the date elements. Attempt code and provide it for analysis if you have issue.

    Don't reference column index to test if combobox has value.

    If Not IsNull(Me.cboSupplier) Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31
    I'm so sorry I uploaded an older version. Feel like a complete idiot. This is the correct version.
    Attached Files Attached Files

  4. #4
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31
    This is the code that's behind the filter button:
    Code:
    '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:    Allen Browne (allen@allenbrowne.com), June 2006.
    Option Compare Database
    Option Explicit
    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.
        '***********************************************************************
        'Text field example. Use quotes around the value in the string.
        If Not IsNull(Me.cboEquipment) Then
            strWhere = strWhere & "([Madeup Code] = """ & Me.cboEquipment & """) AND "
        End If
     
        'Number field example. Do not add the extra quotes.
        If Not IsNull(Me.cboSupplier.Column(1)) Then
            strWhere = strWhere & "([Supplier_Name] = """ & Me.cboSupplier.Column(1) & """) AND "
        End If
        'Text field example. Use quotes around the value in the string.
        If Not IsNull(Me.cboYear) Then
            strWhere = strWhere & "(Year([Purchase_Date]) = """ & Me.cboYear & """) AND "
        End If
     
        'Text field example. Use quotes around the value in the string.
        If Not IsNull(Me.cboMonthYear) Then
            strWhere = strWhere & "(Format([Purchase_Date],'m-yyyy') = """ & Me.cboMonthYear & """) AND "
        End If
     
        'Date field example. Use the format string to add the # delimiters and get the right international format.
        If Not IsNull(Me.txtFromDate) Then
            strWhere = strWhere & "([Purchase_Date] >= " & Format(Me.txtFromDate, conJetDate) & ") AND "
        End If
        
        'Another date field example. Use "less than the next day" since this field has times as well as dates.
        If Not IsNull(Me.txtToDate) Then   'Less than the next day.
            strWhere = strWhere & "([Purchase_Date] <= " & Format(Me.txtToDate, 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.
        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
     
            'Finally, apply the string as the form's Filter.
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
    End Sub
    Private Sub cmdReset_Click()
        'Purpose:   Clear all the search boxes in the Form Header, and show all records again.
        Dim ctl As Control
        
        'Clear all the controls in the Form Header section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox
                ctl.Value = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        
        'Remove the form's filter.
        Me.FilterOn = False
    End Sub
    Private Sub Form_BeforeInsert(Cancel As Integer)
        'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
        'We prevent new records by cancelling the form's BeforeInsert event instead.
        'The problems are explained at http://allenbrowne.com/bug-06.html
        Cancel = True
        MsgBox "You cannot add new clients to the search form.", vbInformation, "Permission denied."
    End Sub
    Private Sub Form_Close()
        Me.Filter = ""
        Me.FilterOn = False
    End Sub
    Private Sub Form_Open(Cancel As Integer)
        'Remove the single quote from these lines if you want to initially show no records.
        'Me.Filter = "(False)"
        'Me.FilterOn = True
    End Sub

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Except for suggested change in post 2, looks good. Time to test. Do you get expected results?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31
    Yes, it seems to be working correctly, not sure why I was having problems with it earlier
    Should I add +1 to "me.txtToDate +1" this:
    Code:
    If Not IsNull(Me.txtToDate) Then   'Less than the next day.
            strWhere = strWhere & "([Purchase_Date] <= " & Format(Me.txtToDate, conJetDate) & ") AND "
        End If
    Will that make it query up to and include the date you enter?


    Don't reference column index to test if combobox has value.

    If Not IsNull(Me.cboSupplier) Then
    The first column is the autonumber would that make a difference?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    The <= and >= operators should be inclusive for the input date. Do you want to capture records for +1 date?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31
    No, just to include the date entered. So leave the +1 out then?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Give it a try. Verify results.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    mayestom is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    31
    Will do. Will work on it more tomorrow. Thank you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filter By Form and Apply Filter Buttons
    By JustLearning in forum Forms
    Replies: 13
    Last Post: 02-18-2015, 01:01 PM
  2. Replies: 2
    Last Post: 02-25-2013, 10:47 AM
  3. Replies: 5
    Last Post: 02-07-2013, 12:21 PM
  4. How to filter dates using an apply filter code.
    By Jgreenfield in forum Reports
    Replies: 4
    Last Post: 11-15-2011, 01:38 PM
  5. Apply Filter based on unbound date boxes
    By anoob in forum Access
    Replies: 3
    Last Post: 01-21-2011, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums