DoCmd.OpenReport之后访问异常终止

时间:2019-06-05 00:38:04

标签: vba ms-access pass-through ms-access-reports

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

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中按“重置”,或者在运行时错误“ 2467”:对话框中按“结束”,则访问将异常终止,而没有任何其他向外的迹象。然后重新打开Access以保存复制的* _Backupx.accdb,并使用.accdb的新副本打开。该错误似乎是标准MS错误: MS Run-time error 2467 正如我所说的,该报告是断断续续的,当它失败时,VB总是在代码中突出显示同一行。如何捕获正在发生的事情,或者可以让VB等待半秒才能尝试写入参数?

2 个答案:

答案 0 :(得分:0)

我记得,打开报告时无法修改字幕。仅在设计模式下。因此,这是不正确的,因为您已经打开了报告

Me.lblFromDate.Caption = strFromDate

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

答案 1 :(得分:0)

最后产生了正确的代码集。单击按钮将创建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。谢谢大家。