【发布时间】:2018-04-29 04:58:40
【问题描述】:
我正在尝试配置一个功能,该功能将根据用户输入他们想要的报告类型和他们想要保存的文件路径自动为用户生成一个 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
【问题讨论】:
-
您是否已经在其他机器上尝试过?
-
是的,我在 2 台机器上尝试过,都遇到同样的错误。
-
我注意到您没有任何错误陷阱——这对于记录失败的报告可能很有用。我会:(a)添加一个错误陷阱并捕获到一个文件中; (b) 在“DoCmd.OpenReport”之前和之后添加 0.2 秒的计时器延迟。如果这有帮助,那么您可以删除全时计时器,将错误捕获在错误陷阱中,并在失败时发出计时器等待。您是否需要在运行时查看这些报告?
-
我也有同样的要求,只针对数千个输出。我每批不能超过 5,000 条记录,而且我有 60,000 多条记录要运行(价值 20 年的数据)。从来没有弄清楚为什么,只能猜测内存被某些东西占用了。这是一个一次性的过程,我的十几批运行也完成了。你的代码看起来很像我的,看不出任何问题的原因。我没有使用定时器延迟。
-
您可以尝试 this method 在单记录报表上运行
DoCmd.OutputTo acOutputReport而之前不使用DoCmd.OpenReport。但我不知道它是否会有所帮助。
标签: vba ms-access for-loop pdf-generation ms-access-2013