DoCmd.OpenReport 后访问异常终止

Access abends after DoCmd.OpenReport

从 VBA 调用报表以接收从 Access 传递查询返回的记录。 DoCmd 完成后,报告的参数将在报告的适当标签容器中设置,并根据需要设置其 .Caption 属性。在此过程中,访问会间歇性地失败,这让我相信报告并未真正打开以接收参数。这是 VBA sub:

Private Sub Report_Open(Cancel As Integer)

    Dim strFromDate     As String
    Dim strToDate       As String
    Dim strWC           As String
    Dim intShift        As Integer
    Dim strSQL          As String

    strFromDate = InputBox("Enter From Date and Time: ")
    strToDate = InputBox("Enter To Date and Time: ")
    strWC = InputBox("Enter Work Center: ")
    intShift = InputBox("Enter Shift: ")

    strSQL = "exec dbo.uspWorkCentreReport_TEST " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"

    CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL

    DoCmd.OpenReport "rpt_qry_ptq_uspWorkCentreReport", acViewReport

    Me.lblFromDate.Caption = strFromDate
    Me.lblToDate.Caption = strToDate
    Me.lblWC.Caption = strWC
    Me.lblShift.Caption = intShift

End Sub

当失败发生时 VBA 突出显示 Me.lblFromDate.Caption = strFromDate。如果我在 VBA 中按 Reset 或在出现 运行-time error '2467': 对话框时按 End,Access 将异常终止,没有任何其他外在迹象。 Access 然后重新打开以保存复制的 *_Backupx.accdb 并打开 .accdb 的新副本。该错误似乎是一个标准的 MS 错误: 正如我所说,该报告是间歇性的,当它失败时 VB 总是突出显示代码中的同一行。我如何捕获正在发生的事情,或者我可以让 VB 在尝试写入参数之前等待半秒?

我记得,打开报告时无法修改标题。仅在设计模式下。所以这是不正确的,因为你已经打开了报告

Me.lblFromDate.Caption = strFromDate

您应该使用文本框而不是标题。您还可以清除边框、填充等,该文本框将显示为标题。

终于生成了正确的代码集。单击按钮创建 strOpenArgs 并将其与 .OpenReport 一起传递。该报告打开并拆分 OpenArgs,并使用更新的标题填充适当的标签。文本框不起作用!这是按钮单击事件:

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

这是报告_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

只要在再次按下表单按钮以再次刷新报表之前关闭报表,每次都会使用新的适当数据打开报表。如果报告未关闭,报告将保留原始数据并且不会刷新新数据...但那是 another question 我要问的。谢谢大家