Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with another query syntax

Posted on 2014-01-26
5
Medium Priority
?
233 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 28

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 28

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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

927 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