Share via

Filter a Report using a Form

Anonymous
2017-10-05T19:02:42+00:00

Hello, 

I'm hoping someone here can help me. I have a database which I want to pull reports from. However, new data is added weekly, so I want to filter the data in the report by certain variables. I'm having issues setting the filter up through. I currently have two textboxes, txtBeginDate and txtEndDate, which I want to use to filter a range of dates. I also currently have a combobox named cboDMA to choose a Market. I have a button (btnFilterMarket) which is supposed to run the filters and then open the filtered report. My query which has the data I need filtered is named qryProduction, the form I want to use to filter is named frmFilters, and the report is named rptProductionNoStores

I've tried a few things, but I just can't get this thing to work...see below for my code as it currently stands: 

Private Sub btnFilterMarket_Click()

    Dim strFilter As String

    If Not IsNull(Me.cboDMA) Then

        strFilter = strFilter & " AND qryPRoduction.DMA = '" & Me.cboDMA & "'"

    End If

    If txtBeginDate <> "" And txtEndDate <> "" Then

        strFilter = strFilter & " AND txtBeginDate >= #" & Format(CDate(Me.txtBeginDate), "mm/dd/yyyy") & "# AND txtEndDate <= #" & Format(CDate(Me.txtEndDate), "mm/dd/yyyy") & "#"

    End If

        If strFilter = "" Then

        Me.Filter = ""

        Me.FilterOn = False

    Else

        Me.Filter = Mid(strFilter, 5)

        Me.FilterOn = True

    End If

    DoCmd.OpenReport "rptProductionNoStores", acViewPreview, "qryPRoduction"

End Sub

Thank you!

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Duane Hookom 26,825 Reputation points Volunteer Moderator
2017-10-05T19:38:18+00:00

Sorry I didn't look at your code very thoroughly. Try replace the date part with : If Not IsNull(Me.txtBeginDate) And Not IsNull(Me.txtEndDate) Then strFilter = strFilter & " AND [Your Date Field] BETWEEN #" & _ Format(CDate(Me.txtBeginDate), "mm/dd/yyyy") & _ "# AND #" & Format(CDate(Me.txtEndDate), "mm/dd/yyyy") & "#" End If

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Duane Hookom 26,825 Reputation points Volunteer Moderator
2017-10-05T19:17:10+00:00

I would change your code to use the WHERE CONDITION of the DoCmd.OpenReport method. The report record source would not contain any of these filters.

Dim strFilter As String strFilter = "1=1 " If Not IsNull(Me.cboDMA) Then strFilter = strFilter & " AND [DMA] = '" & Me.cboDMA & "'" End If If Not IsNull(Me.txtBeginDate) And Not IsNull(Me.txtEndDate) Then strFilter = strFilter & " AND txtBeginDate BETWEEN #" & Format(CDate(Me.txtBeginDate), "mm/dd/yyyy") & _ "# AND txtEndDate <= #" & Format(CDate(Me.txtEndDate), "mm/dd/yyyy") & "#" End If DoCmd.OpenReport "rptProductionNoStores", acViewPreview, , strFilter End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-10-05T19:46:26+00:00

    That works! Thank you so much!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-10-05T19:28:24+00:00

    Ok, so now I'm getting the Enter Parameter Value error for the Begin Date and the End Date...

    Was this answer helpful?

    0 comments No comments
  3. Duane Hookom 26,825 Reputation points Volunteer Moderator
    2017-10-05T19:17:56+00:00

    Please make sure you don't miss line breaks/wrap issues with my reply :-)

    Was this answer helpful?

    0 comments No comments