访问 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.FilterMe.FilterOnLoad 工作,因为从我在MSDN和其他地方看到的一切,它应该工作。话虽如此,我发现我可以使用DoCmd.ApplyFilter来代替:

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

我还是想知道为什么另一种方式的行为如此奇怪,如果有人有任何想法…

正如@David-W-Fenton建议的那样,使用OpenReport的WhereCondition而不是设置Filter表达式。where条件可以与Filter表达式使用的字符串相同。

另外,如果你给OpenReport一个空字符串作为whereconcondition,效果与没有whereconcondition相同,所以这个(未经测试的)代码应该工作,无论你的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

还要注意DoCmd。没有ObjectName参数的OutputTo使用活动对象…

最新更新