vba - 如何以 PDF 格式输出报告,其中名称由字段中的值组成?

标签 vba ms-access report save-as

我想向我的 Access 2007 报告添加功能,通过单击按钮创建报告的 PDF 副本。我知道有一个 OutputTo可以为我执行此操作的宏,但它不允许我将报告字段值作为 PDF 文件名的一部分包含在内,即:

[Client Organisations].Code + "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy") + ".pdf"

虽然我看过这个 MSDN thread而这个 SO question ,我在任何答案中都没有看到使用字段值。

我认为 VBA 代码是可行的方法,所以我(未成功)尝试了以下操作:
Private Sub Create_PDF_Click()
DoCmd.OutputTo acOutputReport, , acFormatPDF, "" + [Client Organisations].Code  
+ "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy")
+ ".pdf", True
End Sub

Run-time error '2465':

Microsoft Office Access can't find the field '|' referred to in your expression



有什么想法吗?

最佳答案

我让它工作(最终)。

以下sub做到了:

Private Sub Create_PDF_Click()

Dim myPath As String
Dim strReportName As String

DoCmd.OpenReport "Invoices", acViewPreview

myPath = "C:\Documents and Settings\"
strReportName = Report_Invoices.[Client Organisations_Code] + "-" +
Report_Invoices.Clients_Code + "-" + Report_Invoices.Invoices_Code + "-" +
Format(Report_Invoices.[Invoice Date], "yyyy") + ".pdf"

DoCmd.OutputTo acOutputReport, "", acFormatPDF, myPath + strReportName, True
DoCmd.Close acReport, "Invoices"

End Sub

两个注意事项:
  • 报告需要在打印前打开。
  • 引用与报告相同名称的字段。那个[Client Organisations].Code[Client Organisations_Code]在报告中。
  • 关于vba - 如何以 PDF 格式输出报告,其中名称由字段中的值组成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2296884/

    相关文章:

    javascript - VBA 和 Javascript bool 表达式有什么区别?

    ms-access - MS Access 错误 :the number of columns in the two selected tables does not match

    sql - Access 2010 中正确的 CASE SELECT 语句是什么?

    VB.Net Crystal 报表连接字符串

    python - 打印 Webkit 报告报错

    sql - Access 分区数据查询并对每个分区求和?

    VBA - 数据验证空单元格

    excel - 调整图片宽度和高度

    php - 如何将这个 php 数组文件解析到 Access 中?

    java - 如何使用 iText Java 将数据附加到现有 pdf 文件