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
Solved

Access VBA: Insert statement using a stored parameter query

Posted on 2013-01-16
5
861 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 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

791 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