Solved

Need help with another query syntax

Posted on 2014-01-26
5
188 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
  • 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 84
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 84

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now