How to Export an Access Report to PDF with a Dynamic File Name
Question details
The user needs to export a filtered Microsoft Access report as a PDF while automatically generating a default file name based on dynamic database fields like album title and artist.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Generating custom PDF reports directly from an Access database and prompting the user with a pre-filled Save As dialog to choose the destination.
- Observed behavior
- The user requires a VBA method to concatenate dynamic fields into a default file name, sanitize it, and pass it to the PDF export function while handling the save dialog.
Ensure you have basic familiarity with Microsoft Access VBA (Visual Basic for Applications). Before running the code, verify that the database fields you intend to use for the file name do not contain invalid characters like slashes or asterisks.
Use VBA and Application.FileDialog to Export Report
Utilize a custom VBA script to open a Save As dialog, pre-fill the dynamic file name, and export the report as a PDF.
By combining the Application.FileDialog object with the DoCmd.OutputTo method, you can dynamically construct a file name from your form fields and prompt the user to select a save location.
Press ALT + F11 to open the VBA editor in Microsoft Access.
Right-click in the Project Explorer, select Insert, and choose Module to write your export script.
Write a helper function using the Replace() method to remove invalid characters (e.g., \, /, :, *, ?, ", <, >, |) from the album title and artist strings.
Create a subroutine that initializes Application.FileDialog(msoFileDialogSaveAs), sets the InitialFileName property by combining your sanitized dynamic fields with a .pdf extension, and applies a PDF file filter.
Inside your subroutine, capture the user's selected file path and use DoCmd.OutputTo acOutputReport, "YourReportName", acFormatPDF, SelectedPath to execute the PDF export.

Looking for a Free Office Suite?
While Microsoft Access is a specialized database tool, WPS Office is an excellent, free alternative for your everyday document, spreadsheet, and presentation needs. It seamlessly converts documents to PDF without the need for complex VBA scripting.

Frequently Asked Questions
How do I remove invalid characters from dynamic file names in VBA?
You can use the Replace() function in VBA to substitute forbidden characters like slashes, asterisks, and question marks with an underscore or an empty string before assigning the file name to the dialog.
Can I export an Access report to Excel instead of PDF using similar code?
Yes, you can use the same FileDialog approach, but you will need to change the acFormatPDF parameter in DoCmd.OutputTo to an Excel format like acFormatXLSX.
Why is my DoCmd.OutputTo exporting the entire table instead of a filtered report?
DoCmd.OutputTo exports the object exactly as it is opened. To export a filtered version, you should open the report invisibly with a WHERE clause using DoCmd.OpenReport first, and then run DoCmd.OutputTo.




