Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build Query that Joins Two Queries

Status
Not open for further replies.

wandan

Technical User
Dec 16, 2001
101
0
0
US
Hello,

I am using Access 2010 with VBA code. I need to create a SQL statement that I can use for docmd.RunSQL (strSQL). I know I have done this in the past but haven't been able to figure it out.

I have Query1 and Query2. Then, I need to do a LEFT JOIN of the two results. Below is basically what I am trying to do. Does anyone know what the proper syntax would be?

SELECT a.id, a.cost, b.invoices FROM
(SELECT id, sum(cost) as cost FROM table1)a
LEFT JOIN
(SELECT id, count(invoices) as invoices FROM table2)b
ON a.id = b.id

Thanks for any help!!!


 
FYI, the DoCmd.RunSQL method should be used only for ACTION queries ...
Furthermore your both subqueries lack the GROUP BY clause.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That makes sense. Currently I have two access queries (not the two in my sample - that was just to illustrate a point) that look to a form for some criteria. Then, I have been trying to create a third one that left joins those two queries. This third query doesn't work because it can't seem to recognize the values from the form. So I turned to VBA to create the SQL on the fly with the value from the form, run the query, then loop to the next record. Is there anyway to replicate this in VBA? Or do you have any suggetions for making the query work?

Thanks again!
 
Open the Form ... Open the query in design view ... right click in the criteria field and select build. goto Forms: open forms: and locate your controls dbl click the found control or hit paste after it has been selected hit OK and the reference to your form has been added to your query.

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
That's what I have now, but it doesn't work. To clarify, it works when I run the query the form reference is in. It doesn't work when I use that query as the source in another query.
 
it will if the form is open

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Thanks for the help. I have narrowed down that it is a Crosstab query that is causing the problem.
 
if its returning no data then its a Join or possibly a GroupBY clause issue as PHV has suggested

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
dhookom :)

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
it is a Crosstab query
So, you have to use the PARAMETERS instruction in the SQL code.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top