Solved

Access VBA: Insert statement using a stored parameter query

Posted on 2013-01-16
5
881 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
[X]
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
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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