How to Run Microsoft Access Query Results as a Batch File Using VBA
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.

- 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 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.
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.
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.
Open the VBA Editor and use the TransferText command to export the query: DoCmd.TransferText acExportDelim, "YourSpecName", "YourQuery", "C:\Path\BatchFile.txt", False.
Directly below the export command, rename the text file using VBA: Name "C:\Path\BatchFile.txt" As "C:\Path\BatchFile.bat".
Run the newly created batch file by calling the Windows Shell: Call Shell("cmd /c ""C:\Path\BatchFile.bat""").

Loop Through the Recordset to Execute Individually
Instead of generating an external file, you can process the query directly in VBA and execute the commands row by row.
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.

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.




