Dave said:
I have several fields that are formatted as "short time"
(e.g. 10:00). I need to create a new field using an
expression that would total these hours. When I try an
expression like "Total Hours: ([Field 1]+[Field 2]+[Field
3])" it will sometimes add correctly, other times not. Is
there a specific funtion for adding hours?
Hi Dave,
I agree with Ken that elapsed time
should be stored as units of time
(like number of seconds), the Date/Time
type is most efficient only at storing
points in time.
Below are results from the Immediate window
that may help understand what is going on:
Imagine you are looking at a record with
the following values for 3 Date/Time fields:
time1=#10:00#
time2=#20:00#
time3=null
and you expect to add them to get "30:00"
-- what is "really stored"
?format(time1,"mm/dd/yyyy hh:nn")
12/30/1899 10:00
?format(time2,"mm/dd/yyyy hh:nn")
12/30/1899 20:00
-- what you get from adding them
?time1 + time2
12/31/1899 6:00:00 AM
?format(time1 + time2,"hh:nn")
06:00
the "sum" was greater than 24 hours,
so the *day* was incremented by 1
and the time of the sum only gets the
remainder.
-- "+" propagates Nulls
?time1 + time2 + time3
Null
-- workaround for any Nulls
?Nz(time1,#00:00#) + Nz(time2,#00:00#) + Nz(time3,#00:00#)
12/31/1899 6:00:00 AM
-- one workaround to get "hours:minutes" for sum
(this should be all in one line...
just replace "timex" with name of your fields)
?Format(DateDiff("n",#00:00#,(Nz(time1,#00:00#)
+ Nz(time2,#00:00#) + Nz(time3,#00:00#)))\60,"00")
& ":" & Format(DateDiff("n",#00:00#,(Nz(time1,#00:00#)
+ Nz(time2,#00:00#) + Nz(time3,#00:00#))) Mod 60,"00")
giving you:
30:00
That's quite a "mouthful"....
it would be a lot more efficient if the
elapsed times had just been stored
as number of seconds (type Long)
as Ken aptly suggested.
If that is not an option, one other workaround
might be to run a "prequery" that computes
the elapsed times, something like:
ElapSecField1: DateDiff("n",#00:00#,Nz([Field1],#00:00#))
ElapSecField2: DateDiff("n",#00:00#,Nz([Field2],#00:00#))
ElapSecField3: DateDiff("n",#00:00#,Nz([Field3],#00:00#))
Then use this "prequery" in 2nd query that computes "formatted sum"
Format((ElapSecField1+ElapSecField2+ElapSecField3)\60,"00")
& ":" & Format((ElapSecField1+ElapSecField2+ElapSecField3) MOD 60,"00")
Note that the "hour Format portion" above
assumes you will never have hour sum > "99".
If that is not the case, then use "000" instead of "00".
Maybe that helps.
Good luck,
Gary Walter