Fix Microsoft Access VBA Code That Exports a Report to PDF
Question details
Fix VBA code that saves a Microsoft Access report as a PDF.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A user wants to build a dated filename and export an Access report to PDF using VBA.
- Observed behavior
- The code can fail because of duplicate variable declarations, invalid filename characters, or incorrect string concatenation.
Open the Access database and identify the exact report name and a writable folder for the PDF. Check the export procedure for duplicate declarations and any filename components containing reserved Windows characters.
Correct the VBA declaration and PDF export filename
Use this approach to fix the procedure and export the report with a valid dated filename.
In Access, press Alt+F11 to open the VBA editor, then open the module containing the report export procedure.
Find each declaration of strFileName in the procedure and keep only one, such as Dim strFileName As String.
Replace or adapt the procedure with the example below. Change Sales Report to the exact name of your report; the PDF will be saved beside the database file. Public Sub ExportReportToPDF() Dim strFileName As String Const reportName As String = "Sales Report" strFileName = CurrentProject.Path & "\Sales_Report_" & Format(Date, "yyyy-mm-dd") & ".pdf" DoCmd.OpenReport reportName, acViewPreview DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, strFileName, False DoCmd.Close acReport, reportName, acSaveNo End Sub
In the VBA editor, select Debug > Compile, correct any remaining errors, and run the procedure. Open the database folder and check the exported PDF.

Export Office documents to PDF with WPS Office
WPS Office is a free, lightweight alternative for working with familiar Office documents and exporting supported files to PDF. Its familiar interface and format compatibility make moving everyday documents straightforward. It does not fix Access VBA code or run Access reports.

Frequently Asked Questions
Why does Access report a duplicate declaration for strFileName?
The variable is declared more than once in the same procedure. Open the procedure in the VBA editor and remove the extra Dim strFileName declaration.
Which characters are invalid in a Windows PDF filename?
Windows filenames cannot contain \ / : * ? " < > or |. Remove or replace these characters in any report title or other value used to build the filename.
How do I add a date to the exported PDF filename?
Use Format(Date, "yyyy-mm-dd") and join it to the filename with the & operator. Avoid date formats containing slashes or colons.
What does DoCmd.OutputTo do in this procedure?
DoCmd.OutputTo exports the named Access report. Use acOutputReport for the object type, acFormatPDF for the format, and a full valid path for the output filename.




