how to run an existing query instead of SQL string using VBA code ?

Posted on 2007-12-06
Medium Priority
Last Modified: 2013-11-27
Hi, here is a simple question. But i just can't figure out.

I made a query in Access Query designer. I want to run the query in VB code for a button in a form. The SQL string is really long so I want to just run the existing query. I was thinking to use DoCmd or db.Execute .....But it didn't work. Please advice....Thank you very much!  Kate
db.Execute ("Q_AppendTesting")      
DoCmd.RunSQL (Q_AppendTesting)
Question by:kate_y
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 93

Accepted Solution

Patrick Matthews earned 600 total points
ID: 20420657
DoCmd.OpenQuery "NameOfQueryGoesHere"
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 total points
ID: 20420667

And to suppress the warning message associated with action queries:


With DoCmd
    .SetWarnings False
    .OpenQuery "QueryName"
    .SetWarnings True
End With

Open in new window

LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 400 total points
ID: 20420670
CurrentDb.Execute "Q_AppendTesting"


Dim db as Database
Set db = CurrentDb()

db.Execute "Q_AppendTesting"

or even better (uses ADO)

Dim cn as adodb.connection
Set cn = CurrentProject.Connection

Dim lRecordsAffected as Long

cn.Execute "Q_AppendTesting", lRecordsAffected

msgbox "You just updated " & lRecordsAffected  & " records.  Woohoo"
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

LVL 26

Expert Comment

ID: 20420843
If this is a Select query, your methods of .execute or .runsql would not work.
LVL 26

Expert Comment

ID: 20420850
^ Above just an explanation. matthew had it right in first comment.

Expert Comment

ID: 20424783

I agree, Matthew has it right.

Author Comment

ID: 20586321
Sorry for the late reply. I traveled outside the country for the past few weeks. Thank you all for the answers. I really appreciate it.


Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question