Access 2007 VBA - 在重新加载报告之前报告过滤器不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6614341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access 2007 VBA - Report filter doesn't work until report is reloaded
提问by ClairelyClaire.msft
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.
我正在使用 VBA 动态加载报告的内容,并且根据从我构建的控制面板表单中选择的报告,报告的查询可能会被过滤。
At the beginning of my Report_Open function, I have this:
在 Report_Open 函数的开头,我有这个:
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 loaded, 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.
我正在使用允许用户查看、导出为 PDF 或打印(在打印预览中打开)的命令按钮加载报告。这些命令都不会导致报告在应用过滤器的情况下打开 - 必须重新加载。
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'twork, 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:
好吧,我不知道为什么Me.Filter
,并Me.FilterOnLoad
没有工作,因为从一切我看到的MSDN和其他地方,它应该工作。话虽如此,我发现我可以使用DoCmd.ApplyFilter
:
'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...
如果有人有任何想法,我仍然想知道为什么另一种方式表现得如此奇怪......
回答by HansUp
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.
正如@David-W-Fenton 建议的那样,将 WhereCondition 与 OpenReport 一起使用,而不是设置过滤器表达式。您的 WhereCondition 可以与您用于过滤器表达式的字符串相同。
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.
此外,如果您为 OpenReport 提供一个空字符串作为 WhereCondition,则效果与没有 WhereCondition 相同,因此无论您是否getGlobal(1)
返回 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.
另请注意,没有 ObjectName 参数的 DoCmd.OutputTo 使用活动对象...在这种情况下将是“测试”报告。