Access 2007 VBA - 报告过滤器在重新加载报告之前不起作用
我正在使用 VBA 动态加载报表的内容,并且根据从我构建的控制面板表单中选择的报表,报表的查询可能会被过滤。
在我的 Report_Open 函数的开头,我有这样的内容:
Private Sub Report_Open(Cancel As Integer)
Me.Filter = "VIP=True"
Me.FilterOnLoad = True
现在,当我开始这个项目时,这是有效的 - 我注释掉了这些行,当取消注释它们时,发现这不再正常工作。我必须重新加载报告才能使过滤器正常工作,而不是在加载报表时应用过滤器 - 通过切换到打印预览并切换回报表视图,或者切换到设计视图然后返回报表视图(在设计视图中,所选过滤器确实显示在属性窗格中)。
我使用命令按钮加载报告,允许用户查看、导出为 PDF 或打印(在打印预览中打开)。这些命令都不会导致报告在应用过滤器的情况下打开 - 必须重新加载它。
我用来加载报告的命令如下供参考:
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
好的,我不知道为什么 Me.Filter
和 Me.FilterOnLoad
不< /strong> 工作,因为从我在 MSDN 和其他地方看到的一切来看,它应该工作。话虽如此,我发现我可以使用 DoCmd.ApplyFilter 来代替:
'Check if VIP - add filter if necessary
If (getGlobal(1) = True) Then
DoCmd.ApplyFilter , "VIP = True"
End If
我仍然想知道为什么另一种方式的行为如此奇怪,如果有人有任何想法......
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 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.
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@David-W-Fenton 建议的那样,将WhereCondition 与OpenReport 一起使用,而不是设置过滤器表达式。您的WhereCondition 可以与您用于过滤器表达式的字符串相同。
另外,如果您为 OpenReport 提供一个空字符串作为WhereCondition,则效果与没有WhereCondition 相同,因此无论您的
getGlobal(1)
是否返回True,此(未经测试的)代码都应该有效。另请注意,没有 ObjectName 参数的 DoCmd.OutputTo 使用活动对象...在本例中它将是“测试”报告。
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.Notice also that DoCmd.OutputTo, without an ObjectName argument, uses the active object ... which will be the "Test" report in this case.