how to combine multiple sql queries in ms access

Posted on 2014-10-07
Medium Priority
Last Modified: 2014-10-07
MS Access
Combining Statements (several into one)

I have several SQL statements...

... and will add others. I would like to execute several in sequence and would like to know the best practice for combining multiple statements in MS Access.
Question by:Henry Gage, Jr.
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
  • 3
  • 2
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 1500 total points
ID: 40365809
Use VBA to call each query in turn.

Author Comment

by:Henry Gage, Jr.
ID: 40365820

Thanks for the comment. Is there also a way to combine the statements within MS Access without using VBA? I reason I am asking is because I want to run several as a group. For example I may want to drop or delete a table, create the table, load the table and export the table with one user event (on click).

Is your larger point that it is better to use VBA combined with MS Access because it enable a developer to govern the user experience by adding better UI controls, thus leading to an application?
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 1500 total points
ID: 40365829
1. Yes - you could use a macro instead of VBA.
2. Not necessarily, because the only ways are: manually executing each query in turn; using a macro; using VBA.
However, it's fairly easy to do it in VBA:

DoCmd.OpenQuery "MyQuery"

Open in new window

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 1500 total points
ID: 40365831
In SQL Server, you could do:
Create Table1 (myfields int)
Create Table2 (myfields int)

but you can't do that in Access.

Author Comment

by:Henry Gage, Jr.
ID: 40366468
I've requested that this question be closed as follows:

Accepted answer: 167 points for Phillip Burton's comment #a40365831
Assisted answer: 167 points for Phillip Burton's comment #a40365809
Assisted answer: 0 points for Henry Gage, Jr.'s comment #a40365820
Assisted answer: 166 points for Phillip Burton's comment #a40365829

for the following reason:

I included my comment as clarification of the questions and to keep a thread context.
LVL 85

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40366204
If you need something like an "all or nothing" scenario, then use Transactions instead of Macros. Transactions allow you to process a group of SQL statements as a whole, and the entire group can be cancelled in the event of a failure of one item.

Using VBA, if you enclose everything in a Transaction, then no changes are made to the live tables until you Commit the transactions. That's not so with Macros, or with calling individual queries via VBA. In other words, without a Transaction it's possible to Delete the Table, but then the CREATE or INSERT statements fail - and you're left with no table!

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

719 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