从 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 错误: 正如我所说,报告是间歇性的,当它失败时,VB 总是突出显示代码中的同一行。如何捕获正在发生的事情,或者我可以让 VB 在尝试写入参数之前等待整整半秒?