As Rick has pointed out the date/time data type is not intended for storing
time duration values. It is in fact a 64 bit floating number under the skin
and implements date/time values as an offset from 30 December 1899 00:00:00.
When you insert a time you are in fact inserting a number of less than 1
corresponding to a point of time on that date.
If you have used a date/time data type column in your table you can sum the
values, but where the sum exceeds 23:59:59 you'll get a date/time value on 31
December 1899 or later, which if formatted as a time will just show the time
of day on that date. You could return it as a time duration value as a
string, however, using the following function, which I've amended slightly
from one which returns the duration between two date/time values:
Public Function TimeDuration( _
dblDuration As Double, _
Optional blnShowDays As Boolean = False) As String
Const HOURSINDAY = 24
Dim lngHours As Long
Dim strMinutesSeconds As String
Dim strDaysHours As String
'get number of hours
lngHours = Int(dblDuration) * HOURSINDAY + _
Format(dblDuration, "h")
' get minutes and seconds
strMinutesSeconds = Format(dblDuration, ":nn:ss")
If blnShowDays Then
'get days and hours
strDaysHours = lngHours \ HOURSINDAY & _
" day" & IIf(lngHours \ HOURSINDAY <> 1, "s ", " ") & _
lngHours Mod HOURSINDAY
TimeDuration = strDaysHours & strMinutesSeconds
Else
TimeDuration = lngHours & strMinutesSeconds
End If
End Function
Paste the above into a standard module in your database and in your report
call it in an expression used as the ControlSource of a text box in a group
or report footer:
=TimeDuration(Sum([Duration]))
If you want to show values of 24 hours or more as # day(s) hh:nn:ss rather
than as hh:nn:ss (with the hours showing as 24, 25 etc) then pass True as the
second optional argument:
=TimeDuration(Sum([Duration]),True)
If you should ever need to insert an individual duration of 24 hours or more
into your table there would be no way you could so with a date/time data type
of course. A solution in such cases would be to have separate columns in the
table for hours, minutes and seconds and combine the values (hours*3600 +
minutes*60 + seconds) to give a total in seconds which you can use for
computations, then format the result back to a hh:nn:ss format by means of a
function which makes use of integer division and the Mod operator.
Ken Sheridan
Stafford, England
HHH said:
Hi,
In my report, I want to sum the time for a particular item. I have a field,
Duration, and it can have anywhere from 5 to 19 lines of time (short time).
Nothing I try seems to work.
Thanks