我想在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
运行时错误'2465':
Microsoft Office Access找不到字段“|”在你的表达中提到
有任何想法吗?
答案 0 :(得分:15)
我得到了它(最终)。
以下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]
。答案 1 :(得分:1)
更容易对字符串进行调暗并将整个表达式放在那里,然后调试它以查看它是否包含有效的报告名称。
像这样:
Dim strReportName as String
strReportName = [Client Organisations].Code
+ "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy")
+ ".pdf"
//then try to print strReportName before you use DoCmd.OutputTo.