Access 2007中VBA - 报告过滤器不工作,直到报告将被重新装入 [英] Access 2007 VBA - Report filter doesn't work until report is reloaded

查看:136
本文介绍了Access 2007中VBA - 报告过滤器不工作,直到报告将被重新装入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用VBA来动态加载报告的内容,并根据该报告是从控制面板的形式我已经建立了选择,该报告的查询可能会被过滤掉。

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.

在我Report_Open功能一开始,我有这样的:

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

现在,当我开始这个项目这是工作 - 我已经注释掉这些行,并取消注释时,他们发现,这并不能正常工作了。相反,过滤器,当加载报表被应用,我必须重新加载报告过滤器的工作 - 无论是通过切换到打印preVIEW和切换回报表视图,或者切换到设计视图,然后回报告视图(在设计视图中,所选择的过滤器不会在属性显示窗格中)。

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).

我使用的命令按钮,允许用户查看,导出为PDF加载报告,或打印(打印preVIEW打开)。这些命令都导致报表与应用过滤器打开 - 它必须被重新加载

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


好吧,我不知道为什么 Me.Filter Me.FilterOnLoad 不要工作,因为从一切,我看到的MSDN和其他地方,它应该工作。话虽这么说,我想通了,我可以使用 DoCmd.ApplyFilter 来代替:


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...

推荐答案

作为@大卫-W-芬顿建议,使用设置过滤器EX pression与OpenReport方法的WhereCondition代替。你WhereCondition可以是你正在使用的过滤器EX pression相同的字符串。

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.

另外,如果你给的OpenReport一个空字符串作为WhereCondition,效果是一样的,因为没有WhereCondition,所以这(未经测试)code应该工作,无论您的 getGlobal(1) 返回True。

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

还要注意的是DoCmd.OutputTo,没有一个ObjectName参数,使用活动对象......这将是在这种情况下,测试的报告。

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

这篇关于Access 2007中VBA - 报告过滤器不工作,直到报告将被重新装入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆