VBA code to Execute Access Query

VBA Code to Execute Access Query
Table of Contents

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
Picture of Anson Antony
Anson Antony
Anson is a contributing author and the founder of www.askeygeek.com. His passion for learning new things led to the creation of askeygeek.com, which focuses on technology and business. With over a decade of experience in Business Process Outsourcing, Finance & Accounting, Information Technology, Operational Excellence & Business Intelligence, Anson has worked for companies such as Genpact, Hewlett Packard, M*Modal, and Capgemini in various roles. Apart from his professional pursuits, he is a movie enthusiast who enjoys spending hours watching and studying cinema, and he is also a filmmaker.

2 Responses

  1. Hello Anson, try to find out how, but I can’t….. (aaargggghhh)

    I have a simple MDB (accessDB) with one table LoginUserDB
    There are 3 fields: UserName UserShortName LastLogIn

    UserShortName = a date field, the other two (UserName & UserShortName) are text fields

    What will be the best way, if I want to search for all:

    UserName = anson.antony

    and update on that all, the field: UserShortName to XXX
    and the field LastLogIn to Format(Now, “DD.MM.YYYY”) (actual date)

    Sorry for bothering you, but I test so different ways without result….

    Thanks in advance
    Pedro

Leave a Reply

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

Congratulations!
You Made It,
Don't Close!

UberCreate Creator Pro Access
for Free!!!

This popup won’t show up to you again!!!

1
Share to...