如何基于MS Access中的记录输出多个PDF文件? [英] How to output multiple PDF files based on record in MS Access?

查看:89
本文介绍了如何基于MS Access中的记录输出多个PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MS Access的新手,现在我有一个基于100条记录的表格的报告.我想根据每个记录输出pdf文件.这意味着每个记录将具有其自己的单个pdf文件,并且文件名将基于记录的列名.

I'm new on the MS Access, now I have a report based on the table with 100 records. I want to output the pdf files based on each record. It means each record will have its own single pdf file and the file name will be based on the record's column name.

我已经在Internet上搜索了,发现此vba代码是可行的.

I have searched on the Internet and I found this vba code is work.

Option Compare Database
Option Explicit

Private Sub Command4_Click()
  Dim rsGroup As DAO.Recordset
  Dim ColumnName As String, myPath As String

  myPath = "C:\test\"

  Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT columnName FROM Table_Name", _
                                    dbOpenDynaset)
  Do While Not rsGroup.EOF
  ColumnName = rsGroup!columnName 

  ' OPEN REPORT, FILTERING RECORDSOURCE BY COLUMN VALUE
  DoCmd.OpenReport "Table_Name_Report", acViewPreview, , "Column='" & ColumnName & "'"
  ' OUTPUT REPORT TO FILE
  DoCmd.OutputTo acOutputReport, "Table_Name_Report", acFormatPDF, _
                                myPath & ColumnName & ".pdf", False
  ' CLOSE PREVIEW
  DoCmd.Close acReport, "Table_Name_Report"

 rsGroup.MoveNext
 Loop

 rsGroup.Close

我从未使用过VBA,并且每次运行此代码时,它将返回一个窗口,并让我输入列记录值​​.那不是自动的,如何更改代码以使其自动读取记录值并填充pdf?

I never used VBA, and every time I run this code, it will return a window and let me input the column record value. That's not automatically, how to change the code to let it read the record value automatically and populate the pdf?

推荐答案

我这样做的方法是仅动态设置报表所基于的查询(即记录源),并带有"columnName".像这样:

The way I would do this is to just dynamically set the query the report is based on (i.e. the recordsource) complete with the "columnName". Something like:

(代码未运行)

Public Sub cmdOpenMyReport_Click()

    Dim strSQL As String
  Dim rsGroup As DAO.Recordset
  Dim ColumnName As String, myPath As String

  myPath = "C:\test\"

  Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT pharmacyName FROM Sfwy_Patients_New", dbOpenDynaset)

  Do Until rsGroup.EOF
    ColumnName = rsGroup!pharmacyName
    strSQL = "SELECT ... WHERE Column = " & COLUMName & ";"  'copy sql from the report record source and put in the column name as a variable
    CurrentDb.QueryDefs("myReportRecordSource").SQL = strSQL 

  ' OUTPUT REPORT TO FILE
  DoCmd.OutputTo acOutputReport, "Sfwy_Patients_New_Report", acFormatPDF, _
                                myPath & ColumnName & ".pdf", False
    rsGroup.movenext
  Loop

end sub

这篇关于如何基于MS Access中的记录输出多个PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆