Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-12-06
7
Medium Priority
?
938 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 93

Accepted Solution

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

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Beware when using the ListIndex and the Column() properties of a listbox in Access 2007.  A bug has been identified in the Access 2007 listbox code which can cause the .ListIndex property to return a -1, and the .Columns(#) property to return a NULL…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

572 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