VBA & Macro Problems

How to Run Microsoft Access Query Results as a Batch File Using VBA

Ayan MasoodAyan Masood Nov 2, 2026 868 views

Question details

The user wants to automate the process of executing command-generating query results from Microsoft Access as a batch file using VBA without manual copy-pasting.

How to Run Microsoft Access Query Results as a Batch File
Product
Microsoft Access
Device & OS
not provided
Scenario
Exporting a query containing DOS commands (such as MKDIR, XCOPY, or RD) and automatically running those commands to manipulate files or directories.
Observed behavior
The user previously copied and pasted query results into Notepad to create a batch file, but now needs an automated method to execute each line of the query programmatically.
Before you start

Before automating command-line tasks, ensure your query generates syntactically correct DOS commands and always test the execution on dummy folders to prevent accidental data loss.

Solution 1Recommended

Export Query to a .bat File and Run via Shell

Use the DoCmd.TransferText method to export the command list to a text file, rename the extension, and run the batch file seamlessly.

This method is highly efficient for running a bulk list of commands. Access exports the generated commands into a text file, which VBA then renames to a .bat file and executes using the Windows Command Prompt.

1
Create an Export Specification

In Access, manually export your query once to a text file to create an Export Specification. This ensures no unwanted quotation marks or commas are added to your batch commands.

2
Write the Export VBA Code

Open the VBA Editor and use the TransferText command to export the query: DoCmd.TransferText acExportDelim, "YourSpecName", "YourQuery", "C:\Path\BatchFile.txt", False.

3
Rename the File to .bat

Directly below the export command, rename the text file using VBA: Name "C:\Path\BatchFile.txt" As "C:\Path\BatchFile.bat".

4
Execute the Batch File

Run the newly created batch file by calling the Windows Shell: Call Shell("cmd /c ""C:\Path\BatchFile.bat""").

Export Query to a .bat File and Run via Shell
Silencing the Command Prompt: If you don't want the black command window to flash on your screen, you can append vbHide to the Shell command: Call Shell("cmd /c ""C:\Path\BatchFile.bat""", vbHide).
Free Microsoft Office alternative

Need a Powerful, Lightweight Office Suite for Data Processing?

While Microsoft Access is highly specialized for databases, a vast majority of complex data manipulation and automation tasks can be handled brilliantly in robust spreadsheets. WPS Office offers a completely free, highly compatible alternative with advanced macro support to streamline your daily workflows.

Fully compatible with Microsoft Excel (.xlsx, .xlsm) formatsLightweight installation and lightning-fast launch timesRobust spreadsheet formulas and advanced macro automation capabilitiesFamiliar ribbon interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why do my XCOPY or MKDIR commands fail when run from VBA?

This usually happens because the file paths contain spaces and are not properly quoted. Ensure your Access query generates paths with double quotes, such as: XCOPY "C:\Source Folder" "D:\Dest Folder".

Do I need administrator privileges to run batch commands from Access?

It depends on the directories you are modifying. If your batch commands are writing to system-protected folders (like C:\Program Files), Access must be run as an Administrator for the commands to succeed.

How do I ensure the batch file is deleted after execution?

You can add a VBA command right after your Shell function to delete the file using: Kill "C:\Path\BatchFile.bat". Note that you may need to introduce a slight delay using the Sleep API to ensure the Shell command finishes running before the file is deleted.