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

时间:2017-11-15 20:09:00

标签: vba ms-access for-loop pdf-generation ms-access-2013

我正在尝试配置一个函数,该函数将根据用户输入他们想要的报告类型和他们想要保存的文件路径,为用户自动生成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

0 个答案:

没有答案