Public Function MissedHours(sTime As Date, fTime As Date, sType As String)
'Declare variables
Dim Hbefore12 As Date 'Will hold the number of hours WORKED on first day of a night shift.
Dim HAfter12 As Date 'Will hold the number of hours WORKED on second day of a night shift.
Dim HoursWorked As Integer 'Will hold the number of hours a staff member worked
Dim shiftLength As Integer 'Will hold the length that the shift is supposed to be.
Dim sStart As Date 'Will hold the time that the staff member started work.
Dim sFinish As Date 'Will hold the time that the staff member finished work
Dim sHBefore12 As Date 'Will hold the number of hours MENT TO BE WORKED on first day of night shift
Dim sHAfter12 As Date 'Will hold the number of hours MENT TO BE WORKED on second day of night shift
'If any values weren't entered, return 0 to prevent errors
If IsNull(sTime) Or IsNull(fTime) Or IsNull(sType) Then
MissedHours = 0
Else
'Work out shift length:
sStart = Nz(DLookup("[ShiftStart]", "ShiftType", "[ShiftType]=""" & sType & """"), #12:00:00 AM#)
sFinish = Nz(DLookup("[ShiftFinish]", "ShiftType", "[ShiftType]=""" & sType & """"), #12:00:00 AM#)
If sStart > sFinish Then 'If start time is greater than end time, then shift is split over 2 days.
sHBefore12 = DateDiff("h", sStart, #11:59:59 PM#) + 1 ' Shift length before 0000 on first day.
sHAfter12 = DateDiff("h", #12:00:00 AM#, sFinish) 'Shift Length After 0000 on 2nd day.
shiftLength = sHBefore12 + sHAfter12
Else
shiftLength = Int(DateDiff("h", sStart, sFinish)) 'Else shift length is the difference between start and finish time.
End If
If sTime > fTime Then 'If StarTime>EndTime it's a night shift over 2 days
Hbefore12 = DateDiff("h", sTime, #11:59:59 PM#) + 1 'Hours worked before 0000 on 1st day
HAfter12 = DateDiff("h", #12:00:00 AM#, fTime) 'Hours worked After 0000 on 2nd day
HoursWorked = Hbefore12 + HAfter12 'Total hours worked over night shift
MissedHours = Int(shiftLength - HoursWorked) 'Return missed hours
Else 'If StartTime<EndTime the shift was over one day
HoursWorked = DateDiff("h", sTime, fTime) 'Total Hours Worked
MissedHours = Int(shiftLength - HoursWorked) 'Return missed hours
End If
End If
End Function