Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access VBA: Insert statement using a stored parameter query

Posted on 2013-01-16
5
Medium Priority
?
914 Views
Last Modified: 2013-01-17
Hi.

In Access 2007, I want to insert rows into a table based on a saved parameter query that involves a union of many SELECT statements.

The query looks like this (+  6 more SELECTs):

SELECT
"DG1" As ["ID"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-2) ,HDB.[GRQty],0)) AS ["prevYR2"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) ,HDB.[GRQty],0)) AS ["prevYR"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["prevYTD"],
Sum(IIf(Year([OrdrDate])=([Enter Year]) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["YTD"]
FROM HDB
WHERE
HDB.[GRQty] <= [Enter Bmax1] AND fmt = "DG"
GROUP BY 1

UNION SELECT
"DG2" As ["ID"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-2) ,HDB.[GRQty],0)) AS ["prevYR2"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) ,HDB.[GRQty],0)) AS ["prevYR"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["prevYTD"],
Sum(IIf(Year([OrdrDate])=([Enter Year]) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["YTD"]
FROM HDB
WHERE
HDB.[GRQty] BETWEEN [Enter Bmin2] And [Enter Bmax2] AND fmt = "DG"
GROUP BY 1

which produces a tidy result set that I would like to insert into a summary table.

I'm having trouble approaching the logic of this -- both what is possible syntax-wise and what is most efficient.

- Is there a way to rewrite this SELECT into an INSERT query?
- Can you execute an INSERT statement  in VBA and call a stored query for the SELECT portion?
- Should I open this query into a recordset and then loop through some insert logic?


Many thanks.
0
Comment
Question by:bishopkd
  • 3
  • 2
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 38784999
something like, this is just the format,

you have to fill in the field names , represented by f1,f2,..fn  in the statement below

Insert into TableX(f1,f2,...Fn)
select a.f1,a.f2, ..a.fn
from
(
SELECT
"DG1" As ["ID"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-2) ,HDB.[GRQty],0)) AS ["prevYR2"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) ,HDB.[GRQty],0)) AS ["prevYR"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["prevYTD"],
Sum(IIf(Year([OrdrDate])=([Enter Year]) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["YTD"]
FROM HDB
WHERE
HDB.[GRQty] <= [Enter Bmax1] AND fmt = "DG"
GROUP BY 1

UNION SELECT
"DG2" As ["ID"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-2) ,HDB.[GRQty],0)) AS ["prevYR2"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) ,HDB.[GRQty],0)) AS ["prevYR"],
Sum(IIf(Year([OrdrDate])=([Enter Year]-1) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["prevYTD"],
Sum(IIf(Year([OrdrDate])=([Enter Year]) AND Month([OrdrDate])<=([Enter Month]) ,HDB.[GRQty],0)) AS ["YTD"]
FROM HDB
WHERE
HDB.[GRQty] BETWEEN [Enter Bmin2] And [Enter Bmax2] AND fmt = "DG"
GROUP BY 1
) as a
0
 

Author Comment

by:bishopkd
ID: 38788016
Thanks. I was trying to make that more complicated.

After I define the parameters, what method do I use to execute the query?
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 2000 total points
ID: 38788081
in VBA,
dim strSQL as string

strSQL="Insert into tableName(<fieldnames here>)" & _
          " select etc

currentdb.execute strSQL, dbfailonerror  '<<<
0
 

Author Comment

by:bishopkd
ID: 38788138
my obj.Execute was failing for data reasons. all is well now.

thanks so much for your help!
0
 

Author Closing Comment

by:bishopkd
ID: 38788142
the syntax fixed my queries
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

885 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