I am currently trying to build a database that calculates the actual working hours it takes to complete a job - so I need to exclude weekends and any time elapse between 6pm and 7.30am.
So for example a job registered 5pm today and cleared at 8.00am tomorrow would show a working time of 1.5 hours.
With a LOT of help I came up with the code below- which works fine if the jobs are cleared in the same day, otherwise ........
Can anyone help with the code ?
Option Compare Database
Option Explicit
'=============================================================================
' Query
'=============================================================================
Public Function GetWaitHours(ByVal pdteReceived As Date, _
ByVal pdteAdopted As Date) As Variant
Const dblcBusinessHours As Double = 10.5
Const tmecDayStart As Date = "7:30:00"
Const tmecDayEnd As Date = "18:00:00"
Dim dblStartHours As Double
Dim dblStopHours As Double
Dim dblReturnHours As Double
On Error GoTo HandleError
If TimeValue(pdteReceived) >= TimeValue(tmecDayStart) _
And TimeValue(pdteReceived) <= TimeValue(tmecDayEnd) _
And TimeValue(pdteAdopted) >= TimeValue(tmecDayStart) _
And TimeValue(pdteAdopted) <= TimeValue(tmecDayEnd) _
And TimeValue(pdteReceived) <= TimeValue(pdteAdopted) Then
dblStartHours = ((TimeValue(tmecDayEnd) - TimeValue(pdteReceived)) * 24)
dblStopHours = ((TimeValue(pdteAdopted) - TimeValue(tmecDayStart)) * 24)
If DateValue(pdteReceived) = DateValue(pdteAdopted) Then
dblReturnHours = (TimeSerial(Hour(pdteAdopted) - Hour(pdteReceived), _
Minute(pdteAdopted) - Minute(pdteReceived), 0) * 24)
Else
Dim cDay As Long
For cDay = CLng(Int(pdteReceived)) To CLng(Int(pdteAdopted))
If cDay = Int(pdteReceived) Then
dblReturnHours = dblReturnHours + dblStartHours
ElseIf cDay = Int(pdteAdopted) Then
dblReturnHours = dblReturnHours + dblStopHours
Else
If Weekday(cDay, vbSaturday) > 2 Then
dblReturnHours = dblReturnHours + dblcBusinessHours
End If
End If
Next cDay
End If
Else
MsgBox Prompt:="Passed dates outside of business hours.", _
Buttons:=vbOKOnly + vbInformation, _
Title:="Error GetWaitHours Function"
End If
ExitHere:
GetWaitHours = dblReturnHours
Exit Function
HandleError:
MsgBox Prompt:=Err.Number & ": " & Err.Description, _
Buttons:=vbOKOnly + vbInformation, _
Title:="Error GetWaitHours Function"
Resume ExitHere
End Function
So for example a job registered 5pm today and cleared at 8.00am tomorrow would show a working time of 1.5 hours.
With a LOT of help I came up with the code below- which works fine if the jobs are cleared in the same day, otherwise ........
Can anyone help with the code ?
Option Compare Database
Option Explicit
'=============================================================================
' Query
'=============================================================================
Public Function GetWaitHours(ByVal pdteReceived As Date, _
ByVal pdteAdopted As Date) As Variant
Const dblcBusinessHours As Double = 10.5
Const tmecDayStart As Date = "7:30:00"
Const tmecDayEnd As Date = "18:00:00"
Dim dblStartHours As Double
Dim dblStopHours As Double
Dim dblReturnHours As Double
On Error GoTo HandleError
If TimeValue(pdteReceived) >= TimeValue(tmecDayStart) _
And TimeValue(pdteReceived) <= TimeValue(tmecDayEnd) _
And TimeValue(pdteAdopted) >= TimeValue(tmecDayStart) _
And TimeValue(pdteAdopted) <= TimeValue(tmecDayEnd) _
And TimeValue(pdteReceived) <= TimeValue(pdteAdopted) Then
dblStartHours = ((TimeValue(tmecDayEnd) - TimeValue(pdteReceived)) * 24)
dblStopHours = ((TimeValue(pdteAdopted) - TimeValue(tmecDayStart)) * 24)
If DateValue(pdteReceived) = DateValue(pdteAdopted) Then
dblReturnHours = (TimeSerial(Hour(pdteAdopted) - Hour(pdteReceived), _
Minute(pdteAdopted) - Minute(pdteReceived), 0) * 24)
Else
Dim cDay As Long
For cDay = CLng(Int(pdteReceived)) To CLng(Int(pdteAdopted))
If cDay = Int(pdteReceived) Then
dblReturnHours = dblReturnHours + dblStartHours
ElseIf cDay = Int(pdteAdopted) Then
dblReturnHours = dblReturnHours + dblStopHours
Else
If Weekday(cDay, vbSaturday) > 2 Then
dblReturnHours = dblReturnHours + dblcBusinessHours
End If
End If
Next cDay
End If
Else
MsgBox Prompt:="Passed dates outside of business hours.", _
Buttons:=vbOKOnly + vbInformation, _
Title:="Error GetWaitHours Function"
End If
ExitHere:
GetWaitHours = dblReturnHours
Exit Function
HandleError:
MsgBox Prompt:=Err.Number & ": " & Err.Description, _
Buttons:=vbOKOnly + vbInformation, _
Title:="Error GetWaitHours Function"
Resume ExitHere
End Function