VBA to Run a Batch File from MS Access (example included)

You can use the following VBA to run a batch file from MS Access:

Shell("Path where your batch file is stored\file name.bat")

In the next section, I’ll explain the full steps to create a VBA to run a batch file.

Steps to create a VBA to run a batch file from MS Access

Step 1: Prepare your batch file

To start, prepare your batch file.

For demonstration purposes, I created a batch file (with the code below) in order to display the current system time. But the method described here would work for any batch file that you may create.

@echo off
time

Next, copy the code into Notepad:

VBA to Run a Batch File from MS Access

Then, save the Notepad as a Batch file by adding the extension of “.bat” to your file name. Here, I chose to save the Notepad file as CurrentTime.bat:

VBA to Run a Batch File from MS Access (example included)

For simplicity, I saved the batch file on my desktop under this path:

C:\Users\Ron\Desktop\CurrentTime.bat

And this is how the batch file would look like:

batch file

Step 2: Add a button in MS Access to run the batch file

In MS Access, go to the Create tab, and then press on the Form Design to create a new form, where you’ll be able to place your button:

Create Tab in MS Access - Form Design

Under the Design tab, press on the “button shape” and then place your button on the form itself:

Add a button in MS Access 2016 to run the batch script

Since we won’t be using the Command Button Wizard, then press Cancel:

Command Button Wizard

On the Property Sheet, select “[Event Procedure]” for the On Click option. Then, press on the 3 dots (“…”)

Property Sheet - MS Access

Note: If the Property Sheet is not visible, you may press F4 to populate this screen.

Step 3: Add the VBA to run the batch file

Now you can use the following template to create the VBA to run the batch file from MS Access:

Shell("Path where your batch file is stored\file name.bat")

In our example, I used the code below to run the CurrentTime.bat batch file from Access:

VBA Shell command to run the batch script when clicking on the button

Step 4: Click on the button to run the batch file

Save your work and then switch to the Form View, where you’ll be able to click on the button to run the batch file from Access:

Form View - MS Access

For our example, once you click on the button, you’ll get the current system time:

Windows Command Prompt