Add an Overwrite Confirmation to Microsoft Access VBA Exports
Question details
Add a confirmation prompt before an Access VBA export replaces an existing file.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A VBA routine exports an Access object with DoCmd.OutputTo, and the user wants to confirm before an existing destination file is replaced.
- Observed behavior
- The export routine does not provide its own overwrite confirmation.
Identify the export routine, the Access object name, and the full destination filename. Make sure the destination folder exists and that you have permission to write to it.
Check the destination file and prompt before exporting
Use Dir to check for an existing file, then call DoCmd.OutputTo only if the file is absent or the user confirms.
In Microsoft Access, open the Visual Basic Editor with Alt+F11 and locate the procedure containing DoCmd.OutputTo.
Adapt this example in your procedure, replacing the report name and path with your own values: Dim outputPath As String outputPath = "C:\Exports\Report.pdf" If Dir$(outputPath, vbNormal Or vbHidden Or vbSystem Or vbReadOnly) <> "" Then If MsgBox("The file already exists. Replace it?", vbYesNo Or vbQuestion, "Confirm Export") <> vbYes Then Exit Sub End If End If DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, outputPath, False
Run the procedure once when the destination file does not exist. Run it again with the file present, and test both No and Yes in the confirmation dialog.

Work with Office documents in WPS Office
WPS Office is a free, lightweight alternative for working with documents, spreadsheets, and presentations. Its familiar interface and Office format compatibility help you move everyday files between suites. It does not replace Microsoft Access or run this Access VBA export routine.

Frequently Asked Questions
Which DoCmd.OutputTo argument specifies the destination file?
The fourth argument is the output filename. Pass the full path to the same variable used in the Dir check.
What happens if I select No in the prompt?
In the example, selecting No runs Exit Sub before DoCmd.OutputTo, so that procedure does not export the file.
Will the prompt appear if the destination file does not exist?
No. Dir returns an empty string when it does not find the file, so the example proceeds directly to DoCmd.OutputTo.
Why might the export fail after I select Yes?
The destination file may be locked or read-only, or you may lack permission to write to the folder. Check the file and folder access before running the export again.




