如何在MS Access中自动化PDF打印各个表单?

问题描述 投票:1回答:1

我正在尝试从表单中为我的数据库中的每条记录打印一个单独的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和FNums,但打印每个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)
...
database forms ms-access access-vba record
1个回答
0
投票

如果FNum是数字,则不应用单引号括起标准值,即:

"Column='" & ColumnName & "'"

应该成为:

"Column=" & ColumnName

此外,除非您的表单上有一个名为Column的字段,否则应将其更改为:

"FNum = " & ColumnName
© www.soinside.com 2019 - 2024. All rights reserved.