MS Access 2013 VBA自动取消OutputTo(错误2501)



我正在尝试配置一个函数,该函数将根据用户输入他们想要的报告类型和他们想要保存的文件路径,自动为用户生成一个pdf文件夹。一些注意事项:

  • 它通常用于基于查询生成约100个pdf报告从链接的后端数据库表中提取数据
  • CReport是数据库中以编程方式分配的报表名称
  • CPath是使用文件对话框选择的文件夹
  • 如果代码循环中的OutputTo行被注释掉,代码可以在一行中完美地执行多次(不幸的是,这就是问题的全部)

错误2501出现在中的2-76个循环重复之间,很少出现在同一点两次偶尔,整个事情都会发生,但我不能放弃只是偶尔运行的东西。-

  • 在Windows 7上使用Access 2013
  • 我尝试过重新构建函数、重新构建表单、反编译、压缩和修复,在输出报告后增加了2秒的延迟,并更改了关闭报告命令的位置

当前代码:

Sub RunThatReport(CReport As String, CPath As String)
'dimension variables
Dim dbs1 As Database, rst As Recordset
Dim Can8 As Double, vend As String
Dim Location As String, CFilePath As String
'create a filename
Select Case CReport
Case "rpt_Form-1"
Location = "_Form-1.pdf"
DoCmd.OutputTo acOutputReport, "rpt_all_lines", acFormatPDF, CPath & "Lines Required.pdf", False
Case "rpt_Form-2"
Location = "_Form-2.pdf"
Case "rpt_Form-3"
Location = "_Form-3.pdf"
Case Else
Exit Sub
End Select
'Get the AN8 of all lines
Set dbs1 = CurrentDb
Set rst = dbs1.OpenRecordset("tbl_all_lines_to_run", dbOpenDynaset)
rst.MoveFirst
'loop through the lines
Do While Not rst.EOF
If Not IsNull(rst!AN8) Then
Can8 = rst!AN8 'I know this assignment is not actually necessary
vend = Replace(Replace(Replace(Trim(rst!Name), "/", "-"), "", "-"), ".", "")
'CFilePath = CPath & "" & vend & Location
CFilePath = CPath & "" & Can8 & Location
Debug.Print vend & ": " & Can8 & " " & CFilePath 'This is my check to look at what was getting hung up
DoCmd.OpenReport CReport, acViewReport, , "ABAN8 =" & rst!AN8 'open report only for that line type
DoCmd.OutputTo acOutputReport, CReport, acFormatPDF, CFilePath, False
'DoCmd.Close acReport, CReport, acSaveNo
'waiting (2) 'This was my delay function
End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
dbs1.Close
DoCmd.Close acReport, CReport, acSaveNo
End Sub

它工作了多年,但在更改为access365后,它显示了这个问题。跳过行";docmd.openreport";在运行命令"0"之前;docmd.outputto";显示了正确的结果。

最新更新