我正在尝试从表单中为我的数据库中的每条记录打印一个单独的pdf文件(开票发票)。
BillingInvoice-
FamilySubDIscSubDIscGrand
Fnum
我试过从这里获取代码:How to output multiple PDF files based on record in MS Access?
这是代码,因为我试图修改它:
Option Compare Database
Option Explicit
Private Sub PrintBtn01_Click()
Dim rsGroup As DAO.Recordset
Dim ColumnName As String, myPath As String
myPath = "C:\test\"
Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT FNum FROM FamilySubDIscSubDIscGrand", _
dbOpenDynaset)
Do Until rsGroup.EOF
ColumnName = rsGroup!FNum
' OPEN FORM, FILTERING RECORDSOURCE BY COLUMN VALUE
DoCmd.OpenForm "BillingInvoice-", acViewPreview, , "Column='" & ColumnName & "'"
' OUTPUT FORM TO FILE
DoCmd.OutputTo acOutputForm, "BillingInvoice-", acFormatPDF, _
myPath & ColumnName & ".pdf", False
rsGroup.MoveNext
Loop
End Sub
我做错了什么。
它按顺序成功保存了pdf和FNum
s,但打印每个FNum
的单个记录,它打印所有记录。所以我最终得到:
FNum001.pdf (all records for db)
FNum002.pdf (all records for db)
Fnum003.pdf (all records or db)
...
但我需要的是:
FNum001.pdf (individual record for FNum001)
FNum002.pdf (individual record for FNum002)
FNum003.pdf (individual record for FNum003)
...
如果FNum
是数字,则不应用单引号括起标准值,即:
"Column='" & ColumnName & "'"
应该成为:
"Column=" & ColumnName
此外,除非您的表单上有一个名为Column
的字段,否则应将其更改为:
"FNum = " & ColumnName