Trying to open a report with a filter

I have this code in the onclick code of a command button on a form...

    Me.Filter = "ListApptBestAgentBusiness" = -1
    Me.FilterOn = True
    DoCmd.OpenReport "rptListingAppointmentsSet", acViewPreview, "", "", acNormal

Open in new window

The report is supposed to be displaying only the records where ListApptBestAgentBusiness is True

But it is displaying all records and not filtering.  Thoughts?
Who is Participating?
PatHartmanConnect With a Mentor Commented:
You are applying the filter to the form that is opening the report, not the report itself.  The report isn't even open yet.  The OpenReport method has two arguments.  One for a filter and the other for a where clause.  The Where clause is more efficient if your BE is connected to a RDBMS since it returns only the selected records and so can minimize network traffic.  The filter option returns ALL records requested by the report's RecordSource and then filters them locally.  So, depending on the size of the recordset the Where clause could save a lot of time.  Personally, I always use the Where Clause regardless of what type of tables I am connected to.

DoCmd.OpenReport "rptListingAppointmentsSet", acViewPreview, , "ListApptBestAgentBusiness = -1", acNormal
SteveL13Author Commented:
Perfect.  Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.