Trying to filter a sub-report

I'm using this code to filter the results in a sub-report.  But it gets "stuck" on the line, "Me.Filter = "[Agent] = '" & strAgent & "'""

Also, I'm not sure if it belongs in the onopen or onload event.

Here is my code:

Private Sub Report_Open(Cancel As Integer)

    If DLookup("[SecurityLevel]", "LOCALtblCurrentUser") > 1 Then
        Dim strAgent As String
        strAgent = DLookup("CurrentUser", "LOCALtblCurrentUser")
        Me.Filter = "[Agent] = '" & strAgent & "'"
        FilterOn = True
    Else
        DoCmd.OpenReport "rptReferrals", acViewPreview, "", "", acNormal
    End If

End Sub

Open in new window

SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
What do you mean by: " it gets "stuck" on" ?
I am not quite sure of your code there, ...perhaps another Expert can help there..

But try it like this, ..as a quick test
"[Agent] =" & "'" & strAgent & "'"
0
Gustav BrockCIOCommented:
You need to set your filter ON:

Me.Filter = "[Agent] = '" & strAgent & "'"
Me.FilterOn = True

Open in new window

/gustav
0
Gustav BrockCIOCommented:
But your code doesn't make much sense ... you are opening a new report in the OnLoad event of a subform ... not very likely what you wish.

/gustav
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jeffrey CoachmanMIS LiasonCommented:
I am sure Gustav can get you sorted...
0
SteveL13Author Commented:
I am opening a report via a command button on a form.  The code behind the command button is:

        Dim Agent As String
        Dim WhereCondition As String
        Agent = Nz(DLookup("[CurrentUser]", "LOCALtblCurrentUser"))
        WhereCondition = "[Agent] = '" & Agent & "'"

        DoCmd.OpenReport "rptReferrals", acViewPreview, , WhereCondition, acWindowNormal

Open in new window


But then I am trying to open the subreport using the same filter with this revised onclick code and it is not working:

        Dim Agent As String
        Dim WhereCondition As String
        Agent = Nz(DLookup("[CurrentUser]", "LOCALtblCurrentUser"))
        WhereCondition = "[Agent] = '" & Agent & "'"
        
        Reports!rptReferrals.Report!subrptReferralCounts.Filter = "[Agent] = '" & Agent & "'"
        Me.FilterOn = True

        DoCmd.OpenReport "rptReferrals", acViewPreview, , WhereCondition, acWindowNormal

Open in new window


I do not know how to filter the sub-report using the same criteria.
0
Gustav BrockCIOCommented:
The first is correct.
The second you could solve extremely easy by setting the Master/Child link fields of the subreport control of the main report to [Agent]

/gustav
0
SteveL13Author Commented:
I tried that.  But it doesn't work because I have a many to one relationship.  Here is a "picture" of the report-subreport I'm trying to create:

Example
0
Gustav BrockCIOCommented:
Then use [AgentName] and not the ID.

Or perhaps this would do:

        Dim Agent As String
        Dim WhereCondition As String
        Agent = Nz(DLookup("[CurrentUser]", "LOCALtblCurrentUser"))
        WhereCondition = "[Agent] = '" & Agent & "'"
        
        DoCmd.OpenReport "rptReferrals", acViewPreview, , WhereCondition, acWindowNormal
        Reports!rptReferrals.Report!subrptReferralCounts.Report.Filter = "[Agent] = '" & Agent & "'"
        Reports!rptReferrals.Report!subrptReferralCounts.Report.FilterOn = True

Open in new window

/gustav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Worked:

        Dim Agent As String
        Dim WhereCondition As String
        Agent = Nz(DLookup("[CurrentUser]", "LOCALtblCurrentUser"))
        WhereCondition = "[Agent] = '" & Agent & "'"
       
        DoCmd.OpenReport "rptReferrals", acViewPreview, , WhereCondition, acWindowNormal
        Reports!rptReferrals.Report!subrptReferralCounts.Report.Filter = "[Agent] = '" & Agent & "'"
        Reports!rptReferrals.Report!subrptReferralCounts.Report.FilterOn = True
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.