VBA & Macro Problems

Add an Overwrite Confirmation to Microsoft Access VBA Exports

Camila MilosovichCamila Milosovich Nov 20, 2026 868 views

Question details

Add a confirmation prompt before an Access VBA export replaces an existing file.

Add an Overwrite Confirmation to Microsoft Access VBA Exports
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the export routine

In Microsoft Access, open the Visual Basic Editor with Alt+F11 and locate the procedure containing DoCmd.OutputTo.

2
Add the file check

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

3
Test both choices

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.

Check the destination file and prompt before exporting
Check the output filename: The fourth argument of DoCmd.OutputTo is the output filename. Use the same full path for the Dir check and the export. A locked, read-only, or inaccessible file may still prevent the export.
Free Microsoft Office alternative

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.

Work with exported PDF files in WPS Office.Open and edit common Microsoft Office formats, including DOCX, XLSX, and PPTX.Use a familiar interface for everyday document work.Move compatible files between Office suites with minimal disruption.
QA img-9

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.