ms-access - 将 MS-Access 报告拆分为 pdf

标签 ms-access vba

我不久前从网上得到了以下VBA代码:

Private Sub btnCreatePDF_Click()
    Dim MyPath As String
    Dim MyFilename As String
    MyPath = "D:\reports\"
        MyFilename = "KS1.pdf"
    'Open report preview and auto-save it as a PDF
        DoCmd.OpenReport "Rpt_KS1", acViewPreview
        DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, False 'Change false to true here to auto-open the saved PDF
    'Close the previewed report
        DoCmd.Close acReport, "Rpt_KS1"
End Sub

它用于在 MS Access 中创建单个 pdf 报告(最多包含 30 页),并且可以很好地满足我的需要。但是,我现在需要将报告分成 30 页左右,并为每个页面创建一个 pdf 文件。知道如何做到这一点吗? 我在报告中有一个“用户名”,或者可以添加一个唯一的 ID(如果这有助于拆分它们等)。

最佳答案

使用 Docmd.OpenReport 的第四个参数(WhereCondition) 。使用WhereCondition,完全按照您在查询中添加Where 时通常执行的操作,只是不包含单词Where。这将使报表仅显示与WhereCondition 匹配的记录。

将唯一标识符列表检索到某种集合或记录集中,然后执行循环。此示例假设您将它们放在一个名为 uniqueIds 的集合中,并且几乎肯定需要您进行一些修改。

Dim MyPath As String
Dim MyFilename As String
MyPath = "D:\reports\"    
'Loop structure may vary depending on how you obtain values
For each uniqueId in uniqueIds
    MyFilename = "KS1" & uniqueId & ".pdf"
'Open report preview and auto-save it as a PDF
    DoCmd.OpenReport "Rpt_KS1", acViewPreview, , "uniqueField = " & uniqueID
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, False
'Close the previewed report
    DoCmd.Close acReport, "Rpt_KS1"
Next uniqueId

严格来说,这可能不会导致每个页面生成不同的 PDF。但它会为每个 uniqueID 生成不同的 PDF,这可能是每个页面,具体取决于您的数据。

关于ms-access - 将 MS-Access 报告拆分为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964074/

相关文章:

Excel VBA - 选择动态单元格区域

sql - 如何使用 MS Access SQL 创建不相等条件?

delphi - 使用 FIREDAC 创建表 MS ACCESS

sql - 在允许通配符月/日但特定年份的 SQL 中查询

regex - 如何使用正则表达式将 VBA 中的多个空格替换为单个空格?

excel - 无法将特定表格从 Web 提取到 Excel VBA (Mac) 中的电子表格中

sql - 相关记录的条件计数

excel - 使用 excel 宏返回所有 Access (.mdb) 表的列表

ms-access - 在不暂停应用程序的情况下使用MsgBox

excel - 必须使用 VBA 在 Excel2010 中复制单元格格式