Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Forcing 0s in Access SQL

Posted on 2011-02-17
5
Medium Priority
?
296 Views
Last Modified: 2012-05-11
Hi.  I have a mini- app written in Access (VBA) in which I loop through about 15 queries and write the results to an Excel template.  The template reads the raw output on a back tab and displays it on visible tabs in a presentable type way through cell references to the data tab.

My issue is this:

In a query similar to this

SELECT
“1” AS Sort,
"HC " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPo
FROM HDB
WHERE …

UNION SELECT
“2” AS Sort,
"TR " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Run_Bind+Actl_Run_Mfg_Print),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Run_Bind+Actl_Run_Mfg_Print) ,0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Run_Bind+Actl_Run_Mfg_Print),0))  AS dPo
FROM HDB
WHERE …

UNION SELECT
“3” AS Sort,
"MM " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_CtnPal_Bind),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_CtnPal_Bind) ,0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_CtnPal_Bind),0))  AS dPo
FROM HDB
WHERE …

When run  it for a vendor who produces HC (1) and MM (3) but no TR (2) the results write out as:
1  HC    100   200  100
3  MM   200   100  100

And if a vendor has no data, the query results do not write out to the spreadsheet at all and writes the next query over its intended destination cells.

So, my question is:  Is there a way to instead output the query results like this:

1  HC    100   200  100
2  TR      0        0       0
3  MM   200   100  100

Or do I need to find a better way to do this all around?

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 40

Expert Comment

by:als315
ID: 34916775
Yes!
Add query (or table) with all products you like to see (HC,TR,MM) and join it in query with your union query (All from created query/table and only existed from union query)
0
 

Author Comment

by:bishopkd
ID: 34917173
I love the simplicity of this.  I'm not getting it to work though.  What am I doing wrong?

SELECT
tblFMT.format,
"HC-disP" AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPa]),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPb]),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPo]),0))  AS dPo
FROM HDB RIGHT JOIN tblFMT
ON HDB.fmt = tblFMT.format
WHERE Year([ProdOrdr Del Date]) = [Enter Year] And
Month([ProdOrdr Del Date]) BETWEEN ([Enter SMonth]) and ([Enter EMonth]) And
HDB.Binder In ([Enter Binder]) AND
HDB.[Contract Narrow Web] In ('Y') AND
HDB.fmt in ('HC')
GROUP BY tblFMT.format,HDB.fmt
ORDER by [fmt]

UNION

SELECT
tblFMT.format,
"TR-disP" AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPa]),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPb]),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPo]),0))  AS dPo
FROM HDB RIGHT JOIN tblFMT
ON HDB.fmt = tblFMT.format
WHERE Year([ProdOrdr Del Date]) = [Enter Year] And
Month([ProdOrdr Del Date]) BETWEEN ([Enter SMonth]) and ([Enter EMonth]) And
HDB.Binder In ([Enter Binder]) AND
HDB.[Contract Narrow Web] In ('Y') AND
HDB.fmt in ('TR')
GROUP BY tblFMT.format,HDB.fmt
ORDER by [fmt]
0
 
LVL 40

Expert Comment

by:als315
ID: 34917395
Resulting query - qry1, table qry - your union query, products - list of all products
DB26828504.mdb
0
 

Author Comment

by:bishopkd
ID: 34918088
This works when I do not have a WHERE Statement.
If the Qry table had a Printer field and your SQL had to look like this:

SELECT
Qry.Product,
nz(sum([Qry]![F1]),0) AS F1,
nz(Sum([Qry]![F2]),0) AS F2,
nz(Sum([Qry]![F3]),0) AS F3
FROM Products LEFT JOIN Qry ON Products.Product = Qry.Product
WHERE Products.Printer IN ('OPM')
Group by Qry.Product;

It no longer pulls in the TR with 0s...
0
 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 34918495
All where should be in separate query. Create additional query for Products
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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