Discussion Locked

Access:converting seconds to time value

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have a field that holds a value representing seconds, I want to total these seconds and report them as hours, minutes and seconds. How do I convert a integer such as 90 to 00:01:30 eg 1 minute 30

5 replies

by peyison

The following function should do what you're looking for.

(Paste it into a code module)
------------------------------
Public Function SecondsToStr(ByVal alNumSeconds As Long) As String
Dim lHours As Long
Dim lMinutes As Long

Const SEC_IN_HOUR = 3600
Const SEC_IN_MIN = 60

lHours = alNumSeconds \ SEC_IN_HOUR
alNumSeconds = alNumSeconds - (lHours * SEC_IN_HOUR)
lMinutes = alNumSeconds \ SEC_IN_MIN
alNumSeconds = alNumSeconds - (lMinutes * SEC_IN_MIN)

SecondsToStr= Format$(lHours, "00") & ":" & Format$(lMinutes, "00") & ":" & Format$(alNumSeconds, "00")

End Function

--------------------------------

It can be used in VBA:

Sub TestIt()
MsgBox SecondsToStr(90)
End Sub

Or from a Query:

SELECT SecondsToStr([NumSeconds]) AS SecStr
FROM Table1;

(Where NumSeconds is the name of the field.

Hope this helps.

by larry h.

While answer 1 is correct, if you simply need to do this once on your report it might be easier to just create a text box and use the following formula. This assumes that your records have a field called DURATION (in seconds) that you are summing for all records.

=Sum([DURATION])/60/60/24
Then format the text box for time.

This discussion is closed to new replies.