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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a union qry

Status
Not open for further replies.

sonikudi

Technical User
Sep 9, 2007
81
0
0
US
Hey,

Okay, i have qry A and qry B..and i want qry C that would do qry A union qry B. The problem is that.. qry A selects 4 cols from tables and qry B select only 3 cols from tables. Both qry A and qry B are aggregate queries. The union of these two queries would have worked great if qry B had 4 cols as well..but it doesn't. When i do a union of qry A and qry B it says they don't have equal number of cols. Is there any way to solve this problem? how can i combine results of qry A and qry B in one table and the rows in qry B not having the 4th col; i would just like those rows to be blank for qry B results. Hope this makes sense. below is the code

Code:
[B] QRY A [/B]
StrSQL = "SELECT t1.[Error Code],t2.Description,COUNT(t1.[PCB SS #]) AS TtlNumberFailed " & _
" FROM [PCB Incoming_Production Results] AS t1, [PCB Error Codes] as t2 " & _
" WHERE t1.[Error Code]<> 'Pass' AND t1.[Error Code]<> 'n/a' AND t1.[Error Code]= t2.[Code] AND Manufacturer = """ & Me.txtMfg & """ And [Date] Between #" & _
   Me.txtMfgStart & "# AND #" & Me.txtMfgEnd & "# GROUP BY t1.[Error Code],t2.Description; "

[B] QRY B [/B]
StrSQL = "SELECT (t1.[Error Code]), count (t1.[Error Code])As TtlNumberFailed " & _
"FROM [PCB Incoming_Production Results] AS t1 LEFT JOIN [PCB Error Codes] ON t1.[Error Code] = [PCB Error Codes].CODE " & _
"WHERE ((([PCB Error Codes].Code) Is Null)) GROUP BY (t1.[Error Code]); "

[B]QRY C[/B]  
StrSQL = "SELECT * FROM qryNoFailedPCBByET UNION SELECT * FROM qryNotHaveED; "

Any help will be just great.

THANK YOU!
 
Add a null column to query A as a fourth.

Bear in mind if you add an order by to the end it will operate as part of the whole query, not an individual part.

John
 
Thanks John.

what is the sql code to add a column? also would i have to output the information into a table or something??
 
To add a column you just add it to the end of the select list, for example:

Code:
StrSQL = "SELECT t1.[Error Code],t2.Description,COUNT(t1.[PCB SS #]) AS TtlNumberFailed, [b]Null As Column4[/b] " & _
" FROM [PCB Incoming_Production Results] AS t1, [PCB Error Codes] as t2 " & _
" WHERE t1.[Error Code]<> 'Pass' AND t1.[Error Code]<> 'n/a' AND t1.[Error Code]= t2.[Code] AND Manufacturer = """ & Me.txtMfg & """ And [Date] Between #" & _
   Me.txtMfgStart & "# AND #" & Me.txtMfgEnd & "# GROUP BY t1.[Error Code],t2.Description; "

Do you have to output the information to a table? No, unless you require the information in a table for your application, but for Access, it can use a query as the source for a report, form etc so there is no need.

John
 
Hey,

actually a column3 needs to be added to QRY B, i think i mislabeled the number of columns in my description the first time. But anywho i did that and when i execute the qry it output the parameter box and asks me for the value of col3.
this is what i entered in the queries under Db window to test it

Code:
SELECT (t1.[Error Code]), count (t1.[Error Code]) AS TtlNumberFailed, NULL as col4
FROM [PCB Incoming_Production Results] AS t1 LEFT JOIN [PCB Error Codes] ON t1.[Error Code] = [PCB Error Codes].CODE
WHERE ((([PCB Error Codes].Code) Is Null)) 
GROUP BY (t1.[Error Code]), col3;
 
okay my bad. i found my mistake. Entered different names for the null column in the qry
 
hey john,

Thanks so much for all the help. This part of the program works.

I still do get the "enter parameter value" dialog box for col3 that access automatically pops up everytime the query is run. Any idea how i disable this pop-up dialog box in my VB code??

thanks!!
 
In fact, the problem was fixed here:
thread701-1431655
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top