根据条件将MS Access报告导出为PDF [英] Export MS Access Report to PDF based on condition
问题描述
是否可以根据报告中的特定条件/字段在MS Access中将报告导出为PDF?
Is there any way to export a report in MS Access to PDF based on a certain criteria/field on the report?
我已经在MS Access中创建了一份生产力报告.除了将50页导出为1个PDF之外,还有没有一种方法可以根据经理的姓名进行导出?经理姓名字段包含在实际报告中.
I have created a productivity report in MS Access. Instead of exporting 50 pages into 1 PDF, is there a way to export based on the manager's name? The field for the managers name is included on the actual report.
推荐答案
您可以采用这个想法并付诸实践.将此插入模块
You can take this idea and play with it. Insert this into a Module
Option Explicit
Dim g_ManagerReportFilterEnabled As Boolean
Dim g_ManagerReportFilter As String
Public Function IsManagerReportFilterEnabled() As Boolean
IsManagerReportFilterEnabled = g_ManagerReportFilterEnabled
End Function
Public Function GetManagerReportFilter() As String
GetManagerReportFilter = g_ManagerReportFilter
End Function
Public Sub ExportFilteredManagerReportToPDF(strManagerName As String)
On Error GoTo ExportFilteredManagerReportToPDF_ErrorHandler
g_ManagerReportFilterEnabled = True
g_ManagerReportFilter = "[MyManagerNameField] = " & Chr(34) & strManagerName & Chr(34)
DoCmd.OutputTo acOutputReport, "MyReportName", acFormatPDF, "MyPath:\MyFileName.PDF", False
GoTo ExitMe
ExportFilteredManagerReportToPDF_ErrorHandler:
Debug.Print err.Number & ": " & err.Description
ExitMe:
g_ManagerReportFilterEnabled = False
Exit Sub
End Sub
并查看您需要替换的变量.这进入您的报告的Report_Open:
and review the variables you need to replace. And this into Report_Open of your report:
Private Sub Report_Open(Cancel As Integer)
If IsManagerReportFilterEnabled = True Then
Me.Filter = GetManagerReportFilter
Me.FilterOn = True
End If
End Sub
因此,此代码尝试解决的问题是我们要使用DoCmd.OutputTo输出我们的PDF,但它不使用Filter参数.因此,我们通过设置两个全局变量(我知道...)来解决此问题,该变量使我们知道是否应使用Manager过滤器以及该过滤器是什么.当我们运行ExportFilteredManagerReportToPDF并传递名称时,该子项会将报告输出为PDF.由于报告附带了代码,因此在运行OutputTo时,报告将检测是否启用了过滤器,如果已启用,则应用它.然后OutputTo完成工作并输出PDF.
So the issue this code is trying to solve is that we want to use DoCmd.OutputTo to output our PDF, but it does not take a Filter parameter. So we work around this by setting up two global variables (I know...) which let us know if we should use the Manager filter and what that filter is. When we run ExportFilteredManagerReportToPDF and pass through a name, the sub will output the report to PDF. Because of the code attached the report, when OutputTo runs, the report will detect whether the filter is enabled, and if it is, apply it. Then OutputTo finishes its work and the PDF is output.
要为经理John Smith运行此程序,例如,您可以从调试窗口运行此程序:
To run this for manager John Smith, say, you can run this from the debug window:
ExportFilteredManagerReportToPDF "John Smith"
这篇关于根据条件将MS Access报告导出为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!