[AccessD] adding Time

Gustav Brock gustav at cactus.dk
Tue Jun 17 06:04:52 CDT 2003


Hi Robert

> I have 7 unbound text boxes that have time amounts listed in HH:MM (8:27)
> format.

> How do I go about adding up the time?  Everything I try, I get a data
> mismatch error...

Your problem is that the textboxes though formatted as time contain
nothing more than text.

You can add these using CDate([txtYourControl]) but you may end up
having more than 24 hours and then the fun begins because you are
"adding" different points in time while you wish to sum spans or
intervals of time. Thus, convert to time intervals like seconds and
add these.

We use this function (watch for line breaks):

<code>

Function TimeAdd(ParamArray astrTimes() As Variant) As Long

' Adds time values and time formatted strings.
' Adds pure numerals as seconds.
' Day, month, and year are stripped as these for time values are assumed to be current date.
' Nulls and badly formatted strings are ignored.
' Returns added times of more than 40 years in total as seconds.

' Example which returns rounded minutes:
' lngMinutes = (TimeAdd("15:34", Null, [Forms]![frmForm]![txtTime], 23, #1:22:10#, "57") + 30) \ 60

' 1999-11-08. Cactus Data ApS. CPH.

  ' Use constants as Long to prevent overruns.
  Const clngSecondsMinute As Long = 60
  Const clngSecondsHour   As Long = 60 * clngSecondsMinute
  
  Dim datTime             As Date
  Dim lngSeconds          As Long
  Dim lngLoop             As Long
  
  ' No special error handling. Ignore excessive inputs.
  On Error Resume Next
  
  For lngLoop = LBound(astrTimes) To UBound(astrTimes)
    If IsDate(astrTimes(lngLoop)) Then
      ' Input is a date/time value or expression.
      datTime = CDate(astrTimes(lngLoop))
      If datTime <> 0 Then
        ' Add seconds, minutes, and hours as seconds.
        lngSeconds = lngSeconds + Second(datTime)
        lngSeconds = lngSeconds + (clngSecondsMinute * Minute(datTime))
        lngSeconds = lngSeconds + (clngSecondsHour * Hour(datTime))
      End If
    ElseIf IsNumeric(astrTimes(lngLoop)) Then
      ' Add a pure numeral as seconds.
      lngSeconds = lngSeconds + astrTimes(lngLoop)
    End If
  Next lngLoop
  
  TimeAdd = lngSeconds

End Function

</code>

/gustav



More information about the AccessD mailing list