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

Posted on 2007-12-06
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 92

Accepted Solution

Patrick Matthews earned 150 total points
ID: 20420657
DoCmd.OpenQuery "NameOfQueryGoesHere"
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 150 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 100 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"
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

690 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