Solved

Need help with another query syntax

Posted on 2014-01-26
5
181 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 26

Expert Comment

by:MacroShadow
Comment Utility
TotHours: Nz([NumberOfHours],0) +Nz([NumberOfHours2],0) +Nz([NumberOfHours3],0)
WHERE PublicActivityCode = "KZU" AND PublicActivityCode2 = "KZU" AND PublicActivityCode3 = "KZU"
0
 
LVL 84
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

743 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

13 Experts available now in Live!

Get 1:1 Help Now