Filtering Reports by date and combobox fields.

AdamAA

Registered User.
Local time
Today, 03:44
Joined
Aug 18, 2012
Messages
24
Hi, I have a form that filters a report based on a date range and it works perfectly. What i'd like to do is modify it so that it also filters by the value selected from a combobox.

Here is the code i'm using that works:

Code:
Private Sub Command1_Click()
'On Error GoTo Err_Handler      'Remove the single quote from start of this line once you have it working.
    'Purpose:       Filter a report to a date range.
    'Documentation: [URL]http://allenbrowne.com/casu-08.html[/URL]
    'Note:          Filter uses "less than the next day" in case the field has a time component.
    Dim strReport As String
    Dim strDateField As String
    Dim strWhere As String
    Dim lngView As Long
    Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Do NOT change it to match your local settings.
    
    'DO set the values in the next 3 lines.
    strReport = "DailyActivityLogs"      'Put your report name in these quotes.
    strDateField = "[TimeOn]" 'Put your field name in the square brackets in these quotes.
    lngView = acViewPreview     'Use acViewNormal to print instead of preview.
    
    'Build the filter string.
    If IsDate(Me.txtStartDate) Then
        strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
    End If
    If IsDate(Me.txtEndDate) Then
        If strWhere <> vbNullString Then
            strWhere = strWhere & " AND "
        End If
        strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ") "
    End If
    
    
    'Close the report if already open: otherwise it won't filter properly.
    If CurrentProject.AllReports(strReport).IsLoaded Then
        DoCmd.Close acReport, strReport
    End If
    
    'Open the report.
    'Debug.Print strWhere        'Remove the single quote from the start of this line for debugging purposes.
    DoCmd.OpenReport strReport, lngView, , strWhere
    
    DoCmd.Close acForm, "DSRPerson", acSavePrompt
    
End Sub

This filters the report by the date ranges I choose. What I would like is for it to also filter by the values in a combobox called "Sub" which relates to a column in my table "DailyActivityLogs" called "Sub" The values selected will be numbers as the row source is a lookup field.
 

Users who are viewing this thread

Back
Top Bottom