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

    "JohnC" <> wrote in message
    news:23e22$51b945ac$40971c7d$...
    > 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
    >
    >
    >
    >
    >
     
    Rob Parker, Jun 14, 2013
    #2
    1. Advertisements

  3. JohnC

    JohnC Guest

    "JohnC" <> wrote in message
    news:23e22$51b945ac$40971c7d$...
    > 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
    >
    >
    >


    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")


    "Rob Parker" <> wrote in message
    news:kpdldm$e3t$...
    > 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
    >
    > "JohnC" <> wrote in message
    > news:23e22$51b945ac$40971c7d$...
    >> 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 14, 2013
    #4
    1. Advertisements

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.
Similar Threads
  1. Andy

    Converting hours/minutes string to minutes

    Andy, Jun 29, 2004, in forum: Access Forms Coding
    Replies:
    6
    Views:
    149
  2. Ron P

    Time in Minutes and Seconds

    Ron P, Mar 18, 2005, in forum: Access Forms Coding
    Replies:
    1
    Views:
    93
    Ken Snell [MVP]
    Mar 18, 2005
  3. Ron P

    Format Time field in only Minutes and Seconds

    Ron P, Mar 19, 2005, in forum: Access Forms Coding
    Replies:
    8
    Views:
    105
    Marshall Barton
    Mar 19, 2005
  4. Tony

    Remove Hours, Minutes and Seconds from Date Field

    Tony, Apr 7, 2005, in forum: Access Forms Coding
    Replies:
    1
    Views:
    234
    Ken Snell [MVP]
    Apr 7, 2005
  5. Anthony

    Returning Minutes and Seconds

    Anthony, Jan 22, 2007, in forum: Access Forms Coding
    Replies:
    2
    Views:
    75
    John Vinson
    Jan 22, 2007
  6. Eris52

    calculating minutes and seconds 4 ways

    Eris52, Aug 11, 2008, in forum: Access Forms Coding
    Replies:
    0
    Views:
    92
    Eris52
    Aug 11, 2008
  7. Maury Markowitz

    FORMAT to show minutes and seconds?

    Maury Markowitz, Apr 21, 2009, in forum: Access Forms Coding
    Replies:
    5
    Views:
    144
    Chegu Tom
    Apr 22, 2009
  8. DubboPete

    Converting minutes to hours:minutes

    DubboPete, Oct 7, 2009, in forum: Access Forms Coding
    Replies:
    4
    Views:
    103
    DubboPete
    Oct 8, 2009
Loading...