VBA & Macro Problems

Use VBA to Export Separate Microsoft Access Reports as PDFs

Huda QurayshiHuda Qurayshi Nov 27, 2026 869 views

Question details

Generate a filtered Microsoft Access report for each tester and export each report to a separate PDF.

Export Separate Microsoft Access Reports as PDFs with VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
A tester query identifies the users who need reports, potentially based on whether an install date is present.
Observed behavior
The desired result is one PDF per tester, containing only that tester's report data.
Before you start

Create a report whose record source includes the text field Tester, and a query named qryTestersToExport that returns the testers who need reports. Create the output folder and confirm the report and query names before running the VBA.

Solution 1Recommended

Loop through testers and export a filtered report for each one

Use a snapshot recordset to open, export, and close the report once for each distinct tester.

This example assumes Tester is a text field in both the query and the report's record source. If your report uses a numeric tester ID, adjust the filter and filename logic to use that ID instead.

1
Prepare the tester query

In Access, create or edit qryTestersToExport so it returns a Tester field. If only testers with an install date need reports, add an Is Not Null criterion under the install-date field.

2
Add the export procedure

Open the Visual Basic editor with Alt+F11, choose Insert > Module, and paste the following code. Replace rptTesterReport and C:\Reports\ with your report name and existing output folder: Public Sub ExportTesterReports() Dim rs As DAO.Recordset Dim tester As String Dim safeName As String Dim pdfPath As String Dim filterText As String Dim badChar As Variant Dim errText As String On Error GoTo ExportError Set rs = CurrentDb.OpenRecordset( _ "SELECT DISTINCT [Tester] FROM [qryTestersToExport] " & _ "WHERE [Tester] Is Not Null", dbOpenSnapshot) Do Until rs.EOF tester = CStr(rs!Tester) safeName = Trim$(tester) For Each badChar In Array("\", "/", ":", "*", "?", Chr$(34), "<", ">", "|") safeName = Replace(safeName, CStr(badChar), "_") Next badChar Do While Len(safeName) > 0 If Right$(safeName, 1) <> "." And Right$(safeName, 1) <> " " Then Exit Do safeName = Left$(safeName, Len(safeName) - 1) Loop If Len(safeName) = 0 Then Err.Raise vbObjectError + 1000, , "A tester has an empty file name." pdfPath = "C:\Reports\Report_" & safeName & ".pdf" If Len(Dir$(pdfPath)) > 0 Then Err.Raise vbObjectError + 1001, , "File already exists: " & pdfPath filterText = "[Tester] = '" & Replace(tester, "'", "''") & "'" DoCmd.OpenReport "rptTesterReport", acViewPreview, , filterText, acHidden DoCmd.OutputTo acOutputReport, "rptTesterReport", acFormatPDF, pdfPath DoCmd.Close acReport, "rptTesterReport", acSaveNo rs.MoveNext Loop rs.Close Set rs = Nothing MsgBox "Report export complete." Exit Sub ExportError: errText = Err.Description On Error Resume Next DoCmd.Close acReport, "rptTesterReport", acSaveNo If Not rs Is Nothing Then rs.Close Set rs = Nothing MsgBox "Export stopped: " & errText, vbExclamation End Sub

3
Run and check the exports

Place the cursor inside ExportTesterReports and press F5. Open the output folder and check several PDFs to confirm that each contains only the intended tester's data.

Loop through testers and export a filtered report for each one
Existing files and filename collisions: The procedure stops rather than overwrite an existing PDF. Different tester names can also produce the same filename after invalid characters are replaced; use a unique tester ID in the filename if needed.
Free Microsoft Office alternative

Work with exported report data in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for working with documents and spreadsheets. It supports common Microsoft Office formats for easier migration, but it does not run Microsoft Access reports or Access VBA.

Review spreadsheet data used alongside exported PDF reportsCompatible with common Microsoft Office formats including DOCX, XLSX, and PPTXFamiliar document and spreadsheet tools ease migrationFree, lightweight suite for everyday office work
QA img-9

Frequently Asked Questions

How can I export reports only for testers with an install date?

Open qryTestersToExport in Design View and enter Is Not Null in the Criteria row for the install-date field. Keep the Tester field in the query output.

Why might a tester's PDF contain other users' data?

Check that the report's record source includes the Tester field and that the field identifies the intended user. The OpenReport WhereCondition must filter that field before OutputTo runs.

What happens if a PDF with the same name already exists?

The example checks the output path with Dir and stops before overwriting the file. Move or rename the existing PDF, or change the naming logic to include a unique tester ID.

Can I use a numeric tester ID instead of a name?

Yes. Include the ID in the query and report record source, then build a numeric WhereCondition without text quotes, such as [TesterID] = 123. You can also include the ID in the PDF filename.