-1

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

4

2 回答 2

0

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

Me.lblFromDate.Caption = strFromDate

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

于 2019-06-05T10:18:10.620 回答
0

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

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

这每次都会使用新的适当数据打开报表,只要在再次按下表单按钮以再次刷新报表之前关闭报表即可。如果报告没有关闭,报告将保持原始数据并且不会用新数据刷新......但这是我要问的另一个问题。谢谢大家。

于 2019-06-06T14:41:20.137 回答