473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Question on Code that calculates hours based on business hours

I have some code in my database that calculates time between two date/time fields and also calculates the weekends and holidays. I did not create this code, so I'm at a bit of a loss why it's not working correctly.

I have to tables, one is called Working Hours, which has two fields, wStart and wEnd. In those fields, I have 08:30 and 18:30. The other table has the Holidays, two fields, hName, which has the name of the holiday, and hDate, which is the date of the holiday.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. Public Function HCalc(CtlS, CtlE, CtlReqCh) As Double
  2. ReqCh = CtlReqCh
  3. If ReqCh = False Or IsNull(CtlS) Or IsNull(CtlE) Then
  4.     HCalc = 0
  5.     Exit Function
  6. End If
  7. StDate = CtlS
  8. EnDate = CtlE
  9. StDateD = Format(StDate, "Short Date")
  10. EnDateD = Format(EnDate, "Short Date")
  11.  
  12.  
  13. If StDateD = EnDateD Then
  14. Result = DateDiff("n", StDate, EnDate, vbUseSystemDayOfWeek)
  15. Else
  16.  
  17. Set qdefH = CurrentDb.CreateQueryDef("", "SELECT * FROM WorkingHours;")
  18. Set rstH = qdefH.OpenRecordset
  19. With rstH
  20. MinDay = DateDiff("n", !wStart, !wEnd, vbUseSystemDayOfWeek)
  21. Result = DateDiff("n", StDateT, !wEnd, vbUseSystemDayOfWeek)
  22. Result = Result + DateDiff("n", !wStart, EnDateT, vbUseSystemDayOfWeek)
  23. .Close
  24.  
  25. StDateT = Format(StDate, "Short Time")
  26. EnDateT = Format(EnDate, "Short Time")
  27. End With
  28.  
  29.  
  30. Set qdefH = CurrentDb.CreateQueryDef("", "SELECT * FROM Holiday WHERE Weekday(hDate)<6 And Year(hDate) Between " & Year(StDateD) & " And " & Year(EnDateD) & ";")
  31. Set rstH = qdefH.OpenRecordset
  32. With rstH
  33.  
  34. StDateD = DateAdd("d", 1, StDateD)
  35.  
  36. Do Until StDateD = EnDateD
  37.     If Weekday(StDateD) < 6 Then
  38.         If .EOF = False Then
  39.             .MoveFirst
  40.             Do Until .EOF
  41.             If StDateD = !hDate Then
  42.                 Result = Result - MinDay
  43.                 Exit Do
  44.             End If
  45.  
  46.         .MoveNext
  47.         Loop
  48.         End If
  49.         Result = Result + MinDay
  50.     End If
  51.     StDateD = DateAdd("d", 1, StDateD)
  52. Loop
  53.  
  54.     .Close
  55.     End With
  56.     Set qdefH = Nothing
  57.     Set rstH = Nothing
  58.  
  59. End If
  60.  
  61. HCalc = Round(Result / 60, 2)
  62. End Function 
  63.  
The code is working, except when we have hours that go from Friday to Monday, it adds an extra 10 hours. During the week it works fine, and from Thursday to Monday it seems to work fine.

I am still a bit of an amature when it comes to writing code, so I can not seem to find the glitch here. I would appreciate any help you can give me! I do not like when a problem is not consistent, and this one is baffling me.

Thanks so much!

Kelly
Feb 10 '10 #1
10 1739
ADezii
8,834 Expert 8TB
I've only had a couple of minutes to look at the code, but try replacing Code Line #37 with the following. If this works, I'll then explain why:
Expand|Select|Wrap|Line Numbers
  1. If Weekday(StDateD) <> 7 And Weekday(StDateD) <> 1 Then 
Feb 10 '10 #2
The calculations seem to be working, but something strange is going on. When I use this code in a query, the results come up, but when I click on the field where the calculation resides, it keeps changing to what is in the record above.

Here's what I put in the query:

cTotalHours: HCalc([oDateSentTc],[oFinalDateT],Yes)

oDateSentTC oFinalDateT cTotalHours
10/2/2009 3:22:04 PM 10/7/2009 1:37:00 PM 30.03
10/2/2009 3:22:13 PM 10/7/2009 1:37:00 PM 28.25
10/5/2009 11:45:00 AM 10/8/2009 11:47:00 AM 28.25
10/5/2009 9:17:12 AM 10/5/2009 3:53:37 PM 6.6
10/5/2009 11:45:00 AM 10/8/2009 11:47:00 AM 30.03
10/5/2009 9:17:12 AM 10/5/2009 3:53:37 PM 6.6

Two minutes later, I do it again:

oDateSentTC oFinalDateT cTotalHours
10/2/2009 3:22:04 PM 10/7/2009 1:37:00 PM 28.25
10/2/2009 3:22:13 PM 10/7/2009 1:37:00 PM 28.25
10/5/2009 11:45:00 AM 10/8/2009 11:47:00 AM 28.25
10/5/2009 9:17:12 AM 10/5/2009 3:53:37 PM 6.6
10/5/2009 11:45:00 AM 10/8/2009 11:47:00 AM 30.03
10/5/2009 9:17:12 AM 10/5/2009 3:53:37 PM 6.6
10/5/2009 11:45:00 AM 10/8/2009 11:47:00 AM 30.03


See how the first calculation changed? If I click on the calculation, sometimes I get 30.03 and sometimes 28.85. It does this all over the place.

Any ideas?

Thanks again.

Kelly
Feb 10 '10 #3
ADezii
8,834 Expert 8TB
Can you Attach the Database, or a Segment of it?
Feb 10 '10 #4
How do I attach here? I could attach the test table that I've been using with the sql from the query.
Feb 10 '10 #5
Here is an example of the code and how it reacts. I have an example of the query. After you run the query, click on the results. The first one changes when you click on it.

Thanks again for all your help!
Attached Files
File Type: zip TestCalcs.zip (37.0 KB, 95 views)
Feb 10 '10 #6
ADezii
8,834 Expert 8TB
Sorry, do not have Access 2007, but can you covert the DB to Access 2000? I'm on vacation right now in Florida, and 2000 is the only Version that I can work with
Feb 10 '10 #7
Thanks so much for taking time out on your vacation!

Here it is in 2000 format.

Hate to sound desperate, but I am a little. :)

Kelly
Attached Files
File Type: zip TestCalcs2000.zip (39.3 KB, 85 views)
Feb 10 '10 #8
ADezii
8,834 Expert 8TB
Kelly, before I look into this, there is one major question. In Post #3 you indicate that you are passing a 3rd Argument to the HCalc() Function (Yes), namely:
Expand|Select|Wrap|Line Numbers
  1. cTotalHours: HCalc([oDateSentTc],[oFinalDateT],Yes)
This Function does not accept a 3rd Parameter, explanation?
Feb 11 '10 #9
I took out the third argument before I sent you the update. We no longer need it.

Kelly
Feb 11 '10 #10
ADezii
8,834 Expert 8TB
but when I click on the field where the calculation resides, it keeps changing to what is in the record above.
Hello LSGKelly, I've tested the Query and could not duplicate what you are expriencing.
Feb 12 '10 #11

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

Similar topics

22
by: Nunaya | last post by:
We have developed two objects called "customer" and "salesrep". For simplicity, assume that these two objects will talk directly to a database. The database has three tables: customers,...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
5
by: mitchchristensen | last post by:
I have a transaction log that tracks issues from a call center. Each time an issue is assigned to someone else, closed, etc. I get a time stamp. I have these time stamps for the beginning of an...
9
by: Adam Monsen | last post by:
I kindly request a code review. If this is not an appropriate place for my request, where might be? Specific questions are in the QUESTIONS section of the code. ...
1
by: Rathtap | last post by:
I am writing an assembly that calculates an expected payment based on a patient's diagnosis and service lines performed in the hospital and the contract set up for the payer -- all this info is...
4
by: CSharpguy | last post by:
I'm not sure if this is the correct forum or not, but I have a basic question. Currently we have are doing calculations via stored procedures and then returning the results back to the client in...
0
by: chrisbenoit06 | last post by:
Howdy...anyone able to assist me in adding the function to calculate the gross pay, and also the calculation and printing of the totals of hours and gross pay....thoughts? /* Purpose: This...
2
by: chrisbenoit06 | last post by:
I'm trying to add a part for 2 times the rate for any hours over the first 60...Can anyone aid me? Many thanks // This program calculates gross pay. #include <iostream> #include <iomanip> ...
10
by: MichiganMan | last post by:
This is a program to just calculate a weekly gross salary based on the hourly wage and hours worked (with overtime too). I am not getting the alert box when it calculates it. Can anyone see what I...
14
by: anchitgood | last post by:
Hey, I have developed the following code and it is executing well on windows. But I don't have LINUX installed on my system. I would be grateful to you guys if you check whether this code is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.