Converting Seconds to Hours, Minutes, Seconds

Discussion in 'Access Forms Coding' started by JohnC, Jun 13, 2013.

  1. JohnC

    JohnC Guest

    So I'm using a procedure to take separate unbound controls for hours,
    minutes, and seconds and storing the computed seconds in a table. This is
    what I'm using:

    Me.txtRaceTimeSeconds = 3600& * Nz(Me.txthours, 0&) + (60& *
    Nz(Me.txtMinutes, 0&) + Nz(Me.txtSeconds, 0&))

    This works fine and I can enter hundreds of a second such as 23.23 in
    txtSeconds and all is good.

    Now when I try to display Hours, Minutes and seconds from the stored Seconds
    field I start having problems. Using:

    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
    Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
    & Format$(Me.[txtRaceTimeSeconds] Mod 60, "00")

    will display everyting properly except if hundreths are entered in seconds,
    i.e. 23.23 the hundreths won't display.

    I've tried just about everything to get the numbers to the right of the
    decimal including:

    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
    Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
    & Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") &
    Format$(Mid(Me.txtRaceTimeSeconds, Len(Int(Me.txtRaceTimeSeconds * 100)) -
    1), "#.00")

    but I get an error if seconds are zero using that. And I can't get properly
    formated HH.MM.SS.ss if there are no hundreths. It drops the .ss
     
    JohnC, Jun 13, 2013
    #1
    1. Advertisements

  2. JohnC

    Rob Parker Guest

    The problem is that the Mod operator rounds floating point numbers to
    integers. Therefore, you need to add the decimal part to your expression,
    after the various Mod operations. This nearly works:

    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
    Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
    & Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") & _
    Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")

    The problem is that it rounds up the seconds in the final Mod operation.
    You can get around this by applying the Int function before that Mod
    operation, so the following works correctly:

    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
    Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
    & Format$(Int(Me.[txtRaceTimeSeconds]) Mod 60, "00") & _
    Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")


    HTH,

    Rob
     
    Rob Parker, Jun 14, 2013
    #2
    1. Advertisements

  3. JohnC

    JohnC Guest

    Got it working!
    Public Sub ConvSecondstoHHMMSS()

    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & _
    ":" & Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & _
    ":" & Format$(Me.[txtRaceTimeSeconds] - (60 * (Me.[txtRaceTimeSeconds] \
    60)), "00.00")

    End Sub
     
    JohnC, Jun 14, 2013
    #3
  4. JohnC

    JohnC Guest

    Thanks Rob,
    Yours works.

    This also works:
    Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & _
    ":" & Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & _
    ":" & Format$(Me.[txtRaceTimeSeconds] - (60 * (Me.[txtRaceTimeSeconds] \
    60)), "00.00")


     
    JohnC, Jun 14, 2013
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.