Kevo wrote:
> 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
You can filter out nonscheduled work hours by having a table of 15
minute "breaks" that include actual 15 minute breaks, lunch breaks and
hours before or after the "shift."
tblI
ID AutoNumber
I Long
ID I
1 1
2 2
3 3
4 4
5 5
6 6
7 7
tblTimeTickets
TTID AutoNumber
PunchIn Date/Time
PunchOut Date/Time
TTID PunchIn PunchOut
1 4/20/2009 11:15:00 AM 4/21/2009 11:30:00 AM
2 4/22/2009 9:30:00 AM 4/22/2009 5:30:00 PM
tblBreaks
BID AutoNumber
BreakStart Date/Time Format h:nn ampm
BreakEnd Date/Time Format h:nn ampm
BID BreakStart BreakEnd
1 12:00 AM 12:15 AM
2 12:15 AM 12:30 AM
....
34 7:30 AM 7:45 AM
35 7:45 AM 8:00 AM
36 10:00 AM 10:15 AM
37 12:00 PM 12:15 PM
38 12:15 PM 12:30 PM
39 12:30 PM 12:45 PM
40 12:45 PM 1:00 PM
41 3:00 PM 3:15 PM
42 5:00 PM 5:15 PM
43 5:15 PM 5:30 PM
....
68 11:30 PM 11:45 PM
69 11:45 PM 12:00 AM
qryBreaks:
SELECT TTID, BreakEnd, PunchIn, PunchOut
FROM tblTimeTickets, tblBreaks, tblI
GROUP BY TTID, BreakEnd, PunchIn, PunchOut,
DateAdd("d",[I]-1,DateValue(PunchIn))+[BreakEnd]
HAVING (((DateAdd("d",[I]-1,DateValue(PunchIn))+[BreakEnd]) Between
[PunchIn] And [PunchOut]));
qryCountBreaks:
SELECT TTID, Count(*) AS CountBreaks
FROM qryBreaks
GROUP BY TTID;
!qryCountBreaks:
TTID CountBreaks
1 66
2 8
qryHoursWorked:
SELECT PunchIn, PunchOut, (DateDiff("n", PunchIn, PunchOut) - 15 *
(SELECT Sum(A.CountBreaks) AS SumOfCountBreaks
FROM qryCountBreaks AS A WHERE A.TTID = B.TTID)) / 60 AS HoursWorked
FROM tblTimeTickets AS B;
!qryHoursWorked
PunchIn PunchOut HoursWorked
4/20/2009 11:15:00 AM 4/21/2009 11:30:00 AM 7.75
4/22/2009 9:30:00 AM 4/22/2009 5:30:00 PM 6
The first time ticket goes from 11:15 am on Monday until 11:30 am on
Tuesday. The time worked is 11:15 am to 12 pm, 1 pm to 3 pm, 3:15 pm to
5 pm, 8 am to 10 am, 10:15 am to 11:30 am for a total of 7.75 hours.
The second time ticket goes from 9:30 am on Wednesday until 5:30 pm on
the same day. The time worked is 9:30 am to 10 am, 10:15 am to 12 pm, 1
pm to 3 pm, 3:15 pm to 5 pm for a total of 6 hours.
A purer way, in my view at least, to write qryBreaks is:
qryBreaks:
SELECT TTID, BreakEnd, PunchIn, PunchOut
FROM tblTimeTickets, tblBreaks, tblI
GROUP BY TTID, BreakEnd, PunchIn, PunchOut, DateAdd("n", DateDiff("n",
TimeValue(CDate(0)), [BreakEnd]), DateAdd("d",[I]-1,DateValue(PunchIn)))
HAVING DateAdd("n", DateDiff("n", TimeValue(CDate(0)), [BreakEnd]),
DateAdd("d",[I]-1,DateValue(PunchIn))) Between [PunchIn] And [PunchOut];
I'm not sure about how to exclude weekend hours using just SQL in a
simple way. Perhaps the following VBA will give you some ideas:
http://groups.google.com/group/comp....8d33651b087c05
James A. Fortune
(E-Mail Removed)
SWEEPER - A train that stops at all stations. -- Dictionary of
Australian Slang, Second Edition, Sidney J. Baker, 1943 (Price: Three
shillings & sixpence)