Solved

how to combine multiple sql queries in ms access

Posted on 2014-10-07
6
1,045 Views
Last Modified: 2014-10-07
MS Access
SQL
Combining Statements (several into one)

I have several SQL statements...
CREATE TABLE A...
INSERT INTO TABLE A...
CREATE TABLE B...
INSERT INTO TABLE B...
CREATE TABLE C...
INSERT INTO TABLE C...

... 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.
0
Comment
Question by:Henry Gage, Jr.
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

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

Author Comment

by:Henry Gage, Jr.
ID: 40365820
Hello,

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?
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 375 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

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 24

Assisted Solution

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

but you can't do that in Access.
0
 

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.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now