使用更新的参数集无法从 VB 关闭或刷新访问报表

Posted

技术标签:

【中文标题】使用更新的参数集无法从 VB 关闭或刷新访问报表【英文标题】:Access report cannot be closed or refreshed from VB with updated parameter set 【发布时间】:2019-06-06 15:36:25 【问题描述】:

单击表单按钮会打开一个包含数据的访问报告。这些参数与对返回记录的 SQL 存储过程的传递查询一起使用。报告没有出现模态,我希望它保持这种状态。但是,如果用户在返回表单并尝试设置新参数之前没有关闭报表,则报表会在后台保持打开状态,单击按钮后,报表会以旧参数和数据出现并且不会刷新带有新的参数/数据。

一种选择是对报表使用模态,但这会导致用户不得不主动关闭报表的粗略过渡。另一种选择是在重试期间关闭报告,这是我一直在尝试的。我试过了:

    If CurrentProject.AllReports(rpt_ptq_uspWorkCentreReport).IsLoaded Then
        DoCmd.Close acReport, rpt_ptq_uspWorkCentreReport, acSaveNo

在几个不同的位置:_MousedDown,作为 _Click 中的第一个 If,以及 _BeforeInsert。每次 CurrentProject.AllReports(rpt_ptq_uspWorkCentreReport).IsLoaded 在报告位于后台并且正在使用下一次尝试新参数重新处理表单时,在第二次传递期间出现错误。同样在第二次尝试期间,.OpenReport 行失败并出现 SQL 错误,因为 strSQLP1 不完整。这是 _Click 事件:

Private Sub btnPreviewP1_Click()

    If (Me.txtToDateP1 < Me.txtFromDateP1) Then
        MsgBox ("The From Date must occurr before the To Date!")
    End If

    Dim strFromDateHMS  As String
    Dim strToDateHMS    As String
    Dim strSQLP1    As String
    Dim strOpenArgs As String

    strFromDateHMS = Format(Me.txtFromDateP1, "yyyy-mm-dd") & " " & Me.cboFromHourP1 & ":" & Me.cboFromMinuteP1 & ":" & Me.cboFromSecondP1
    strToDateHMS = Format(Me.txtToDateP1, "yyyy-mm-dd") & " " & Me.cboToHourP1 & ":" & Me.cboToMinuteP1 & ":" & Me.cboToSecondP1

    strSQLP1 = "exec dbo.uspWorkCentreReport '" & strFromDateHMS & "','" & strToDateHMS & "','" & strWCP1 & "'," & strShiftP1

    strOpenArgs = Me.RecordSource & "|" & strFromDateHMS & "|" & strToDateHMS & "|" & strWCP1 & "|" & strShiftP1

    ' This line is all that's needed to modify the PT query
    CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQLP1

    DoCmd.OpenReport "rpt_ptq_uspWorkCentreReport", acViewReport, , , , strOpenArgs

End Sub

还有 .AllReports 当前所在的 _MouseDown:

Private Sub btnPreviewP1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If CurrentProject.AllReports(rpt_ptq_uspWorkCentreReport).IsLoaded Then
        DoCmd.Close acReport, rpt_ptq_uspWorkCentreReport, acSaveNo
    End If
End Sub

这是 Report_Open:

Private Sub Report_Open(Cancel As Integer)
    Dim SplitOpenArgs() As String
    SplitOpenArgs = Split(Me.OpenArgs, "|")
    Me.lblFromDate.Caption = SplitOpenArgs(1)
    Me.lblToDate.Caption = SplitOpenArgs(2)
    Me.lblWC.Caption = SplitOpenArgs(3)
    Me.lblShift.Caption = SplitOpenArgs(4)
End Sub

【问题讨论】:

CurrentProject.AllReports().IsLoaded是要走的路,但AllReports需要一个字符串作为参数。 OPTION EXPLICT在模块顶部会避免该错误!见***.com/q/1139321/9439330 CurrentProject.AllReports("rpt_ptq_uspWorkCentreReport").IsLoaded 给出正确的结果。没有引号,vba 假定rpt_ptq_uspWorkCentreReport 是一个未声明的变量,隐式转换为空字符串"",导致CurrentProject.AllReports("").IsLoaded 的结果为假,因为没有加载名为"" 的报告;) Option Explicit 在每个模块的顶部!! 【参考方案1】:

为什么不在 OpenReport 之前关闭报告?我修改了你的代码:

Private Sub btnPreviewP1_Click()

    If (Me.txtToDateP1 < Me.txtFromDateP1) Then
        MsgBox ("The From Date must occurr before the To Date!")
    End If

    Dim strFromDateHMS  As String
    Dim strToDateHMS    As String
    Dim strSQLP1    As String
    Dim strOpenArgs As String
    Dim R

    strFromDateHMS = Format(Me.txtFromDateP1, "yyyy-mm-dd") & " " & Me.cboFromHourP1 & ":" & Me.cboFromMinuteP1 & ":" & Me.cboFromSecondP1
    strToDateHMS = Format(Me.txtToDateP1, "yyyy-mm-dd") & " " & Me.cboToHourP1 & ":" & Me.cboToMinuteP1 & ":" & Me.cboToSecondP1

    strSQLP1 = "exec dbo.uspWorkCentreReport '" & strFromDateHMS & "','" & strToDateHMS & "','" & strWCP1 & "'," & strShiftP1

    strOpenArgs = Me.RecordSource & "|" & strFromDateHMS & "|" & strToDateHMS & "|" & strWCP1 & "|" & strShiftP1

    ' This line is all that's needed to modify the PT query
    CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQLP1

    ' Check if report is open and close it without saving:
    For Each R In Reports
        If R.Name = "rpt_ptq_uspWorkCentreReport" Then
            DoCmd.Close acReport, "rpt_ptq_uspWorkCentreReport", acSaveNo
            Exit For
        End If
    Next R

    DoCmd.OpenReport "rpt_ptq_uspWorkCentreReport", acViewReport, , , , strOpenArgs

End Sub

【讨论】:

以上是关于使用更新的参数集无法从 VB 关闭或刷新访问报表的主要内容,如果未能解决你的问题,请参考以下文章

使用数据集时无法正确绑定水晶报表,显示参数缺失

如何使用绑定数据集自动更新访问中的报表?

来自数据集的 Vb.Net 水晶报告

关于VB.NET中 datagridview 刷新问题

POWER BI 支持一个报表连接多数据集

使用onbeforeunload事件检测窗口是否刷新或关闭 .