Public Function TimeElapsed(ByVal dtmTime As Date, strMinSec As String, _
Optional ByVal blnShowdays As Boolean = False) As String
' Returns a date/time value as a duration
' in hours etc or days:hours etc if optional
' blnShowDays argument is True.
' Time format is determined by strMinSec argument,
' e.g. "nn" to show hours:minutes,
' "nn:ss" to show hours:minutes:seconds,
' "" to show hours only
' Call the fucntion, in a query for example, like this:
' SELECT EmployeeID,
' TimeElapsed(SUM(TimeDurationAsDate(TimeStart, TimeEnd)), "nn") As TotalTime
' FROM TimeLog
' GROUP BY EmployeeID;
Dim lngDays As Long
Dim strDays As String
Dim strHours As String
Dim IsNegative As Boolean
' if duration is a negative value then flag as IsNegative
' and convert to a positive value
If dtmTime < 0 Then
IsNegative = True
dtmTime = Abs(dtmTime)
End If
' get whole days
lngDays = Int(dtmTime)
strDays = CStr(lngDays)
' get hours
strHours = Format(dtmTime, "hh")
If blnShowdays Then
TimeElapsed = lngDays & ":" & strHours & Format(dtmTime, ":" & strMinSec)
Else
TimeElapsed = Format((Val(strDays) * 24) + Val(strHours), "#,##0") & _
Format(dtmTime, ":" & strMinSec)
End If
' remove trailing colon if necessary
If Right(TimeElapsed, 1) = ":" Then
TimeElapsed = Left(TimeElapsed, Len(TimeElapsed) - 1)
End If
' add minus sign if duration is a negatve value
If IsNegative Then
TimeElapsed = "-" & TimeElapsed
End If
End Function