473,473 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

conditional calculations in queries

11 New Member
I have a query listing: Date, Session Length, StaffName1, StaffName2, StaffName3

I'd like the next column to be Total Staff Hours, being a straightforward calculation of length x the number of staff working that session.

sometimes we don't have a second and third staff member and those columns will return blank.

How can I return the calculation based on these conditions?
Apr 20 '07 #1
5 2186
Lysander
344 Recognized Expert Contributor
I have a query listing: Date, Session Length, StaffName1, StaffName2, StaffName3

I'd like the next column to be Total Staff Hours, being a straightforward calculation of length x the number of staff working that session.

sometimes we don't have a second and third staff member and those columns will return blank.

How can I return the calculation based on these conditions?
I would write a global function that took the length and the 3 staff names, with the staffnames being optional, and then calculated and returned the value wanted. Then call this function in your query.
Apr 20 '07 #2
phillyon
11 New Member
please could you expand on how to go about that, I'm not very experienced on Access
Apr 20 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
I have a query listing: Date, Session Length, StaffName1, StaffName2, StaffName3

I'd like the next column to be Total Staff Hours, being a straightforward calculation of length x the number of staff working that session.

sometimes we don't have a second and third staff member and those columns will return blank.

How can I return the calculation based on these conditions?
This is why the StaffNames should not be listed like this. Have a look at this tutorial

Normalisation and Table structures
Apr 21 '07 #4
Lysander
344 Recognized Expert Contributor
please could you expand on how to go about that, I'm not very experienced on Access
As mmccarthy suggests, the tables should be normalised, but a function to solve your problem would go like this. Put the following function in a global module
Expand|Select|Wrap|Line Numbers
  1. Function intTotalHours(intLength As Integer, varName1 As Variant, varName2 As Variant, varName3 As Variant) As Integer
  2. 'you have to use variant type because you are expecting nulls
  3. 'I am assuming hours are integers
  4. Dim intCount As Integer
  5. intCount = 0
  6. If Not IsNull(varName1) Then intCount = intCount + 1
  7. If Not IsNull(varName2) Then intCount = intCount + 1
  8. If Not IsNull(varName3) Then intCount = intCount + 1
  9. intTotalHours = intLength * intCount
  10. End Function
  11.  
and then put the function into your query like this.
Expand|Select|Wrap|Line Numbers
  1. SELECT Date, Session Length, StaffName1, StaffName2, StaffName3,intTotalHours([Session Length],StaffName1, StaffName2, StaffName3) AS Total_Staff_Hours
  2. FROM tablename;
  3.  
Hope that helps
Apr 23 '07 #5
phillyon
11 New Member
thanks for all your helps, it has helped me approach it for the future but has made my life more difficult in the immediate.

I understand why normalising is good now.

I would like to do this, but how can I present the information. for instance, normalised I guess I would have another table, SESSIONSTAFF which contains

TableID, SessionID, StaffID
1 - 12 - Phil
2 - 12 - Stu
3 - 13 - Craig

This is sure useful for my initial question of how to report on how many hours have been worked and who's worked them etc.

However, I now have the problems of inputting the info? In my session form, can I open a staffing subform for that session and use tick boxes?

I also have the new problem of displaying that information on my day to day forms.

Is there a query format where I can lookup the staff say for session no. 12 and it gives me a text string of "Phil and Stu" or session 13 it would just say "Craig" etc.

Any develpoment ideas welcome. Thanks everyone.
Apr 24 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: chandy | last post by:
Hi, I am trying to change an SP from dynamic SQL to proper SQL but I can't figure a way to conditionally add extra parts to the statement. How can I do the equivalent of the following? ...
2
by: Scot Niklos via AccessMonster.com | last post by:
Sorry if it is way to much information but I'm trying to cover all bases Access 97 A= combobox that queries Distributor B= combobox that limits customers by distributor in a C= combobox that...
6
by: Marc Jennings | last post by:
Hi there, I am trying to get some conditional functionality into some of my apps. I can use the format : private void doStuff() { //do something }
4
by: tlyczko | last post by:
Hello, I have read about currency calculations, etc. in this newsgroup, and I understand that currency math will be sufficiently accurate for what I need to do for a mileage/expense report. ...
22
by: robertmeyer1 | last post by:
Hi. I have been working on creating the structure of a DB and now I am trying to create some queries which will perform calculations. I have 1 table, tblAnswers which is based off an append query....
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
6
by: Jared | last post by:
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE = CASE WHEN @Name IS NULL THEN ELSE...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
22
by: Rickster66 | last post by:
As Instructed this is a new thread regarding my original post: "Select Only 10 Columns Going Back" I'm sorry for the late response. I've been gathering up information and carefully with as much...
2
by: AdamOnAccess | last post by:
Below is the SQL to a query that combines "Sum", "Group By" and "Count". In the case below, the field "ad group" is supposed to be counted and appear in a new field called "CountOfadGroupId". The...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.