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

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
e.g.
db.Execute ("Q_AppendTesting")      
DoCmd.RunSQL (Q_AppendTesting)
kate_yAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Patrick MatthewsConnect With a Mentor Commented:
DoCmd.OpenQuery "NameOfQueryGoesHere"
0
 
Patrick MatthewsConnect With a Mentor Commented:
kate_y,

And to suppress the warning message associated with action queries:


Regards,

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

Open in new window

0
 
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
CurrentDb.Execute "Q_AppendTesting"

or

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"
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
jerryb30Commented:
If this is a Select query, your methods of .execute or .runsql would not work.
0
 
jerryb30Commented:
^ Above just an explanation. matthew had it right in first comment.
0
 
incrediblejohnCommented:

I agree, Matthew has it right.
0
 
kate_yAuthor Commented:
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.

k
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.