Solved

Need help with another query syntax

Posted on 2014-01-26
5
212 Views
Last Modified: 2014-01-29
In query designer... I wan to have a field that reads something like this but I can't get the syntax right.

TotHours: Nz([NumberOfHours],0) where PublicActivityCode = "KZU" +Nz([NumberOfHours2],0) where PublicActivityCode2 = "KZU" +Nz([NumberOfHours3],0) where PublicActivityCode3 = "KZU"

How do I write that line to make it work?
0
Comment
Question by:SteveL13
[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
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39810550
TotHours: Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0)
WHERE PublicActivityCode = "KZU" AND PublicActivityCode2 = "KZU" AND PublicActivityCode3 = "KZU"
0
 
LVL 85
ID: 39810595
Are these all fields in the same table? Or 3 different tables?

Do you want to sum them ONLY when PublicActivityCode, PublicActivityCode2 and PublicActivityCode3 are "KZU"? What about where one of those value is NULL, for example? Would you still want to sum all 3?

Personally, I'd do that in a separate query and join it back to your other query (and be sure to add in the ID value needed to join the two).

So in a separate query:

SELECT (Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0) AS TotalHours WHERE PublicActivityCode = "KZU" AND PublicActivityCode2 = "KZU" AND PublicActivityCode3 = "KZU"

If you need to sum them regardless of whether one or more is NULL:

SELECT YourIDField, (Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0) AS TotalHours WHERE PublicActivityCode = "KZU" OR PublicActivityCode2 = "KZU" OR PublicActivityCode3 = "KZU"

Save that as a stored query (perhaps named qryTotalHours), then include qryTotalHours in your main query. Join them on YourIDField, and then drag the TotalHours field into the query grid.
0
 

Author Comment

by:SteveL13
ID: 39811914
Yes, I need to sum them regardless of whether one or more is NULL.  But I'm getting a syntax error with:

SELECT YourIDField, (Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0) AS TotalHours WHERE PublicActivityCode = "KZU" OR PublicActivityCode2 = "KZU" OR PublicActivityCode3 = "KZU"
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39811952
SELECT YourIDField, Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0) AS TotalHours WHERE PublicActivityCode = "KZU" OR PublicActivityCode2 = "KZU" OR PublicActivityCode3 = "KZU"
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39811973
You'd have to change YourIDField to reflect the Field name from your table. I don't know what that field would be, but as I mentioned earlier, it would be the field you'd use to join your new stored query back to the other ...
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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