开发者

Access 2007 VBA - Report filter doesn't work until report is reloaded

开发者 https://www.devze.com 2023-03-18 13:56 出处:网络
I\'m using VBA to dynamically load the content of a report, and depending on which report is selected from the control panel form I\'ve built, the report\'s query might be filtered.

I'm using VBA to dynamically load the content of a report, and depending on which report is selected from the control panel form I've built, the report's query might be filtered.

At the beginning of my Report_Open function, I have this:

Private Sub Report_Open(Cancel As Integer)
    Me.Filter = "VIP=True"
    Me.FilterOnLoad = True

Now, this was working when I started this project - I had commented out these lines, and when uncommenting them discovered that this doesn't work properly anymore. Instead of the filter being applied when the report is loade开发者_运维问答d, I have to reload the report for the filter to work - either by switching to print preview and switching back to report view, or by switching to design view and then back to report view (in design view, the selected filter does display in the properties pane).

I am loading the report using command buttons that allow the user to view, export to PDF, or print (opens in print preview). None of these commands cause the report to open with the filter applied - it has to be reloaded.

The commands I'm using to load the report are below for reference:

If (Action = "View") Then
    DoCmd.OpenReport "Test", acViewReport
ElseIf (Action = "PDF") Then
    DoCmd.OutputTo acOutputReport, "Test", acFormatPDF
ElseIf (Action = "Print") Then
    DoCmd.OpenReport "Test", acViewPreview
End If

Ok, I have no idea why Me.Filter and Me.FilterOnLoad don't work, since from everything I have seen on MSDN and elsewhere, it should work. That being said, I figured out that I can use DoCmd.ApplyFilter instead:

'Check if VIP - add filter if necessary
If (getGlobal(1) = True) Then
    DoCmd.ApplyFilter , "VIP = True"
End If

I'd still like to know why the other way was behaving so oddly, if anyone has any ideas...


As @David-W-Fenton suggested, use the WhereCondition with OpenReport instead of setting a Filter expression. Your WhereCondition can be the same string you were using for the Filter expression.

Also, if you give OpenReport an empty string as the WhereCondition, the effect is the same as no WhereCondition, so this (untested) code should work whether or not your getGlobal(1) returns True.

Dim strWhereCondition As String
Dim strReport As String
strReport = "Test"
If (getGlobal(1) = True) Then
    strWhereCondition = "VIP = True"
End If

Select Case Action
Case "View"
    DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
Case "PDF"
    DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
    DoCmd.OutputTo acOutputReport, , acFormatPDF
Case "Print"
    DoCmd.OpenReport strReport, acViewPreview, , strWhereCondition
End Select

Notice also that DoCmd.OutputTo, without an ObjectName argument, uses the active object ... which will be the "Test" report in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号