Solved

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

Posted on 2007-12-06
7
776 Views
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
e.g.
db.Execute ("Q_AppendTesting")      
DoCmd.RunSQL (Q_AppendTesting)
0
Comment
Question by:kate_y
7 Comments
 
LVL 92

Accepted Solution

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

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 150 total points
ID: 20420667
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
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 20420670
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 26

Expert Comment

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

Expert Comment

by:jerryb30
ID: 20420850
^ Above just an explanation. matthew had it right in first comment.
0
 
LVL 3

Expert Comment

by:incrediblejohn
ID: 20424783

I agree, Matthew has it right.
0
 

Author Comment

by:kate_y
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.

k
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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.

825 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