DoCmd.OutputTo在列表中的不同项目上间歇性失败 - 错误2501

问题描述:

DoCmd.OutputTo命令在包含错误2501的列表中的不同项目上失败。下图为我的子例程(删除了大量代码)。它通过供应商列表循环,通过电子邮件发送发票。我们正在将Access 365与链接表一起运行到SQL Server 2008R2。每次运行例程时,代码都会在其他供应商上失败(有时不会失败)。每个传出电子邮件都有一个附件。每个附件都有一个唯一的文件名。将附件写入硬盘以避免网络滞后。该报告相当复杂,有时会调用命中链接表的函数。需要关于如何使功能更强大而不会失败的帮助。DoCmd.OutputTo在列表中的不同项目上间歇性失败 - 错误2501

Sub EmailInvoices() 
    InvFilePath = "C:\temp\" 
    On Error GoTo EmailInvoicesProblem 

    ' loop through the list of suppliers, this invoice type, this date 
    For x = 1 To SupplierCount 

    SQLStr3 = "SELECT * from " & RptTypeDtlQry & WhereClause 
    DoCmd.OpenReport RptName, acViewDesign, , , acHidden 
     Set Inv_rpt = Reports(RptName) 
     Inv_rpt.RecordSource = SQLStr3 
     Inv_rpt.Caption = "Supplier " & rst!supplier_code & " " & InvType & " Invoices" 
    DoCmd.Close acReport, RptName, acSaveYes 

    'image the invoice, attach the invoice to the email, and then delete the image 
    InvFileName = InvFilePath & rst!supplier_code & ".pdf" 
    TryAgain: 
    DoCmd.OutputTo acOutputReport, RptName, acFormatPDF, InvFileName, False 
    objOutlookMsg.Attachments.Add (InvFileName) 
    fso.DeleteFile (InvFileName) 

    ' send all the invoices of this supplier, this invoice type, this date in a single email. 
    objOutlookMsg.Send 

    NextSupplier: 
    rst.MoveNext 
Next x 
rst.Close 
Exit Sub 

End Sub 'EmailInvoices() 
+0

错误2501通常意味着报表中没有数据 - 您可以捕获错误并报告Err.Description - 您的代码似乎在上面的文本中被分割 - 您可以使用记录集预处理SQL,并首先确定是否有任何记录在尝试创建报告之前 – dbmitch

也许报表文件名有无效字符? (会发布这个作为评论,但没有足够的分数)

你可以把一些Debug.Print语句显示变量是什么时候它的工作,当它不?