It would be difficult to do this without using VBA; it would need an
expression of Byzantine complexity. A VBA function is a lot easier. The
following function should do it. All you need to do is paste it into a
standard module in your database:
Public Function TimeWorked(dtmJobStart As Date, _
dtmJobFinish As Date, _
dtmDayStart As Date, _
dtmDayFinish As Date) As Double
Dim dtmDate As Date
Dim lngMinutes As Long
Dim lngMinutesInDay As Long
lngMinutesInDay = DateDiff("n", dtmDayStart, dtmDayFinish)
For dtmDate = DateValue(dtmJobStart) To DateValue(dtmJobFinish)
If Weekday(dtmDate, vbMonday) < 6 Then
If dtmDate = DateValue(dtmJobStart) Then
If TimeValue(dtmJobStart) > dtmDayStart Then
lngMinutes = DateDiff("n", TimeValue(dtmJobStart),
dtmDayFinish)
Else
lngMinutes = lngMinutesInDay
End If
ElseIf dtmDate = DateValue(dtmJobFinish) Then
If TimeValue(dtmJobFinish) < dtmDayFinish Then
lngMinutes = lngMinutes + _
DateDiff("n", dtmDayStart, TimeValue(dtmJobFinish))
Else
lngMinutes = lngMinutes + lngMinutesInDay
End If
Else
lngMinutes = lngMinutes + lngMinutesInDay
End If
End If
Next dtmDate
TimeWorked = lngMinutes / 60
End Function
Make sure you save the module with a different name to the function, e.g.
mdlTimeStuff.
You can then call it, passing the start and end date/time values for the job
and the start and end times for the working day, e.g. in the 'field' row of a
query in design view (all as one line)
HoursWorked:TimeWorked([Available date], [Despatch date],
#06:00:00#,#20:00:00#)
If you try it in the debug window, again as a single line, with your sample
values:
? TimeWorked(#2008-04-02 07:40:14#, #2008-04-03
09:15:21#,#06:00:00#,#20:00:00#)
This returns 15.5833333333333.
Note that date literals must not be in the UK short date format of
dd/mm/yyyy. The US format of mm/dd/yyyy can be used, but I prefer the
unambiguous ISO standard date format of YYYY-MM-DD.
The function does assume continuous time on each day from start to finish,
and does not allow for break time. It also does not allow for overtime if
work starts before 06.00 or finishes after 20.00 on any of the days. It
does, however, exclude weekends from the computation.
Ken Sheridan
Stafford, England
Kevo said:
I am fairly new to Access and cannot use VBA.
I normally use Access to work with quantities only, but I am now trying to
calculate time differences and have become stuck.
I am trying to work out the "actual" time it takes to complete an order.
Using the expression
ateDiff("n",[Available date],[Despatch date])/60
But this doesn't take in to consideration our Working days (Monday to
Friday) or working times (06:00 to 20:00).
Say [Available date] = 02/04/2008 07:40:14
and [Despatch date] = 03/04/2008 09:15:21
I get the answer as 25.6 hours. But the "actual" time is only 15.6 hours.
Some orders can take 5 or 6 days to complete.
I need to calculate the total hours actually worked to complete the order
Can anybody please help?
Best Regards,
Kevo