VBA code to Execute Access Query

VBA code to Execute Access Query (with Image)

What is it?

This VBA code helps you to run the Access query from any of the VBA supported tools like MS Excel, Word etc.

Why is it?

You can avoid the process of opening the DB and executing the query manually; this code will execute even from an Excel sheet without opening the Access DB manually.

If you use the Import Access Query Results code discussed in the previous post you even get the query results to Excel, everything in just one click.

VBA Code to Import Access Table/Query data to Excel

How to use VBA code to Execute Access Query

Step 1: Copy Paste the below code in your module.


Dim myDB As Database
Set myDB = OpenDatabase(“C:\MYDATABASE.mdb”) ‘Mention the DB path and Replace the ‘MYDATABASE’ with your Access DB name
myDB.Execute “myQuery”  ‘Replace ‘myQuery with your Access Query name
myDB.close ‘Use this only if you want to close DB


Step 2: Click the Run button or F5 to Execute the Access Query.

VBA code to Execute Access Query


About Anson

I’m Anson having a decade of versatile experience in Business Process Outsourcing, Finance & Accounting, Information Technology, Operational Excellence & Business Intelligence. I have got the opportunity to work for Genpact, Hewlett Packard, M*Modal and Capgemini in various roles and responsibilities, starting from an Associate to a Manager. Learning anything new has always been my passion and if related to Business and Technology then I love it. asKeygeek.com is an outcome of my passion towards the same and I have been into asKeygeek since 2012. Outside business and technology I’m a movie buff who spends hours together watching and learning Cinema, I do make Short Films 🙂

One Comment

  1. Pingback: VBA code to Execute Access Query (with Image) |...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.