问题
Is there any way to export a report in MS Access to PDF based on a certain criteria/field on the report?
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.
回答1:
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
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
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.
To run this for manager John Smith, say, you can run this from the debug window:
ExportFilteredManagerReportToPDF "John Smith"
来源:https://stackoverflow.com/questions/24453238/export-ms-access-report-to-pdf-based-on-condition