I'm very new to creating union queries. I understand the purpose but not how to properly implement it for my purpose. So far I've created to perfectly fine crosstab queries. One shows input on a date ordered, the other shows the out put on a date completed.
I am trying to combine these two charts into one chart. The code I've got so far will only show one date. I am looking to have 2 (4 if you count the break down into the two material types) line graphs.
Here is the code for this so far.
Input: - TRANSFORM Sum(tblOrders.QtyOrdered) AS SumOfQtyOrdered
-
SELECT tblOrders.DateOrdered, Sum(tblOrders.QtyOrdered) AS [Total Of QtyOrdered]
-
FROM tblOrders
-
GROUP BY tblOrders.DateOrdered
-
PIVOT tblOrders.MaterialType;
Output: - TRANSFORM Sum(tblOrders.QtyCompleted) AS SumOfQtyCompleted
-
SELECT tblOrders.DateCompleted, Sum(tblOrders.QtyCompleted) AS [Total Of QtyCompleted]
-
FROM tblOrders
-
GROUP BY tblOrders.DateCompleted
-
PIVOT tblOrders.MaterialType;
-
Union: - SELECT * FROM [qryTPInput_Crosstab]
-
UNION SELECT * FROM [qryTPOutput_Crosstab]
-
As always, Thanks in Advance!
14 3621 - Convert your Input and Output Crosstab Queries into Make Table Queries.
- Create a UNION Query based on these 2 Tables.
I tried that and it still only returns the DateOrdered and the sum of qtyOrdered. I am still missing the DateCompleted and the QtyCompleted -
SELECT * FROM [tblTPInput]
-
UNION SELECT * FROM [tblTPOutput];
Still needing help towards a solution
Can you Upload the Database, not really sure what is goining on here, or I am missing something very obvious.
Thank you for looking into this!
Unzip and rename to .mdb
The two crosstabs I have are:
qryTPInput_crosstab
qryTPOutput_crosstab
I have since removed the non working union query and changed the crosstabs back from the original idea of make table queries.
NeoPa 32,556
Expert Mod 16PB
I doubt this can work (reliably at least).
One of the main concepts of a CrossTab query is the number of columns is determined by the data rather than the design, whereas a precondition of UNIONing two queries is that they both have the same number, and type, of fields. Does that sound like a situation that can work together to you guys?
@bknabl - You are fortunate in that you have one of the most highly qualified Experts, and also Moderator, who is very adept in this specific area, and who subscribed to this Post. I am referring to NeoPa. @NeoPa - Both Crosstabs output the same number of Fields whose Data Types are also exactly the same. I honestly do not see why a MAKE TABLE/UNION Query would not work under these exact conditions, but it does not seem to. @bknabl - I guess that we could create 2 Recordsets based on the Crosstabs, then populate a Results Table. This would surely guarantee that the resulting Values are correct, but I'll hold off on this for now and wait and see what NeoPa or other Members have to say on the matter.
NeoPa 32,556
Expert Mod 16PB
I've pretty well shot my bolt on this one ADezii. I have only used CrossTabs a few times in my whole career I'm afraid.
I suspect though, if you're falling over issues with this, then Access is not refusing at the point where it checks the output of the CrossTabs, but rather earlier on in the parsing where it simply says - "This is a CrossTab. No way am I gonna get caught with my pants down on this one! I'm not even touching it."
I must say that would probably be my approach if I were designing a database system to fit where Access does.
I checked in help, and though it's not very definitive on the subject, it does say the allowed sources are any of :
Table
QueryDef
SELECT statement
While the CrossTab can be stored in a QueryDef, so can other types of query (such as UPDATE or INSERT). I would read that to mean only QueryDefs consistent with the other SQL statements listed. In that case only SELECT is listed. TRANSFORM is not, so I would read that to be saying UNION doesn't support including CrossTabs. I may be misreading it of course, as it's not too explicit, but that would be my reading of it.
Thanks for your insight into this matter, NeoPa. It is always valued and appreciated. @bknabl - I'm going to attempt a Code-based approach on this dilemma and see what happens. Stay tuned to this station (LOL).
I do believe that I have arrived at a solution to your dilemma, though nothing like what was initially envisioned. - Create a Table named tblResults consisting of the following Fields:
- [Date] {DATE}
- [Quantity] {LONG}
- [ALUM] {LONG}
- [STEEL] {LONG}
- Copy-N-Paste the following Code to wherever you deem necessary, then Execute it.
- Dim MyDB As Database
-
Dim rstInput As DAO.Recordset
-
Dim rstOutput As DAO.Recordset
-
Dim rstResults As DAO.Recordset
-
-
'Clear the Results (tblResults) Table
-
CurrentDb.Execute "DELETE * FROM tblresults;", dbFailOnError
-
-
'Setup the 3 required Recordsets, namely: Input, Output, and Results
-
Set MyDB = CurrentDb
-
Set rstInput = MyDB.OpenRecordset("qryTPInput_Crosstab", dbOpenForwardOnly)
-
Set rstOutput = MyDB.OpenRecordset("qryTPOutput_Crosstab", dbOpenForwardOnly)
-
Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
-
-
'Append the Results of the Input Crosstab Query to tblResults
-
With rstInput
-
Do While Not .EOF
-
rstResults.AddNew
-
rstResults![Date] = .Fields(0)
-
rstResults![Quantity] = .Fields(1)
-
rstResults![ALUM] = .Fields(2)
-
rstResults![STEEL] = .Fields(3)
-
rstResults.Update
-
.MoveNext
-
Loop
-
End With
-
-
'Clean up the Input Recordset
-
rstInput.Close
-
Set rstInput = Nothing
-
-
'Append the Results of the Output Crosstab Query to tblResults
-
With rstOutput
-
Do While Not .EOF
-
rstResults.AddNew
-
rstResults![Date] = .Fields(0)
-
rstResults![Quantity] = .Fields(1)
-
rstResults![ALUM] = .Fields(2)
-
rstResults![STEEL] = .Fields(3)
-
rstResults.Update
-
.MoveNext
-
Loop
-
End With
-
-
'Clean up the Input and Results Recordsets
-
rstResults.Close
-
rstOutput.Close
-
Set rstResults = Nothing
-
Set rstOutput = Nothing
-
-
'Let's see the results
-
DoCmd.OpenTable "tblResults", acViewNormal, acReadOnly
- Forget everything that I just told you, and download the Attachment to get a better Picture of exactly what is going on.
- Any questions, feel free to ask.
Wow, you have no idea how much I appreciate all this hard work from you guys/gals. However the solution you provided me I believe is only adding the input to the output. I am not trying to do that. I am mearly trying to display both the input and the output on a single graph. Or perphaps I am doing something wrong?
Sorry, but now I am really confused. Kindly Post the results of what the so called UNION of qryTPInput_Crosstab and qryTPOutput_Crosstab should be. I have a feeling that you are referring to a Summation rather than a UNION, but until I see the resultant data, I'll just wait and see. - qryTPInput_Crosstab
-
DateOrdered SumOfQtyOrdered ALUM STEEL
-
8/10/2010 326 318 8
-
- qryTPOutput_Crosstab
- DateCompleted SumOfQtyCompleted ALUM STEEL
-
0 0 0
-
8/10/2010 58 58
-
8/11/2010 55 55
-
- Result DataSet?
Hi all. I too am confused.
ADezii showed the results of running each crosstab separately in post 13 above. There are only two separate dates within the data supplied - 10 August and 11 August. One row of the output query returns a null date and 0 values, as ADezii's post shows.
I find that unioning these two crosstab queries works just as expected.
Executing - SELECT * FROM qryTPInput_Crosstab
-
UNION
-
SELECT * FROM qryTPOutput_Crosstab;
returns 1 + 3 rows as per the two input queries: - DateOrdered SumOfQtyOrdered ALUM STEEL
-
0 0 0
-
10/08/2010 58 58
-
10/08/2010 326 318 8
-
11/08/2010 55 55
I cannot see much that can be done with these rows at all, so it all seems to be a bit misguided as an approach. As for putting these in line charts (post #1) - line charts should be used to plot continuous functions or values which can be treated to all intents and purposes as continuous (total sales by month, for example). What have these values got to do with a continuous function?
By the way, NeoPa was right to question unioning crosstabs as an approach; it can only work in the special case where the number of columns returned by the crosstabs is equal, and in most crosstab applications this is highly unlikely, except where all queries involved in the Union have exactly the same set of column properties applied.
-Stewart
NeoPa 32,556
Expert Mod 16PB
It's interesting that, from Stewart's post, it is possible to UNION CrossTabs, at least as long as they are used as subqueries. IE. You may not use UNION ==> TRANSPOSE, but you can use UNION ==> SELECT * FROM (TRANSPOSE...).
This would be the logical equivalent of UNION ==> stored QueryDef of a CroosTab.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: s_gregory |
last post by:
The mdb is considerable size 70 +- mb. A complex union query was
working well, but when an additional union select... was added into
the query, selecting identical fields from a different source,...
|
by: DeanL |
last post by:
Hi All,
Having a problem developing an Access 97 database in Access 2003. The
database needs to go out to a number of staff who will review it and
recommend any changes but when I convert the...
|
by: Carlos777 |
last post by:
I have made a database on access 2003 and it's running good but when I open it on Access 2007 the forms open's very smal (about 1x1 cm).
thank you in advance.
Carlos
|
by: daniel aristidou |
last post by:
hi i made a union query to join about 9 tables together. however when i press run an error message apears saying that it could not find object: ".
Any way so i started again and ran it after every...
|
by: dohminator |
last post by:
Hello Experts,
I have two systems written in Access 2003 that I'm in the process of merging. A user will be given a specific screen depending on his user name. I have a form (let's call it...
|
by: rdsandy |
last post by:
Hi,
I have some code below in VBA for Access 2003 which is on a button called "RentalCrosstabPercTtlQtyMonthLoc_Click". This is a crosstab query which brings up rental items down the side, who...
|
by: Jorgo |
last post by:
In Access 2003 under Windows XP
I have made a database for the local Legacy group and they require reports on Widows of various ages.
I have their age calculated and shown in form view, however...
|
by: PW |
last post by:
I have a client who decided to Add/Remove Programs that haven't been
used. Besides Microsoft Works 2002, I'm not sure what else she
removed. When she opens up our Access 2003 MDE application, she...
|
by: JOCOPVA |
last post by:
i try to open up microsoft access 2003 and i get an error message it reads
A system error occurred, or there isnt enough free memory to start. Close unneeded programs and tyry again.
I have...
|
by: paulquinlan100 |
last post by:
Hi
I'm trying to tweak the query below so that the second SELECT only
pulls out one record per "Site_Ref", i.e. the record that has the
maximum "appraisal_date" out of the 4 Appraisal_types. Is...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |