Results 1 to 4 of 4
  1. #1
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118

    Sum Durations of Time


    I have records with a time field formatted as hh:nn:ss. I want to sum that field in a report. However anytime the sum is over 24 hours the hours are not showing. For instance if the sum =24 hours, 8 minutes and 32 seconds (24:08:32) then my result is 0:8:32. Is there a way to total the hours correctly?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You don't have a sum problem, you have a display problem. You really should be storing a value representing number of minutes or whatever the appropriate interval is. This may help:

    https://docs.microsoft.com/en-us/off...-datetime-data

    You can use the Int() function to grab the integer portion of your summed value, which would be the number of days.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118
    A user on anther forum was able to help me. He helped me create a custom function to use when ever I needed to sum durations. It works exactly like I needed it to work.
    Here is the function:
    Option Compare Database
    Option Explicit
    Public Function AllTimeSum(t As Long) As String
    Dim H As Long, M As Long, S As Long
    H = Int(t / 3600)
    M = Int(t / 60) - (H * 60)
    S = t - (H * 3600) - (M * 60)
    AllTimeSum = Format(H, "0") & ":" & Format(M, "00") & ":" & Format(S, "00")
    End Function

    And here is the Control Source for the field on the report that sums the durations.
    =AllTimeSum(Sum((Hour([Actual])*3600)+(Minute([Actual])*60)+Second([Actual])))

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I'm glad you have a solution. Personally I would have written it so you just pass the value of Actual (or the sum of it), and let the function do the rest. I'm lazy, I don't want to have to remember all the stuff you have to do there to call the function. By the look of the result you want there, the ElapsedTime function in the link I gave you could easily have been adapted to return the result. That's why I posted it. I just tested and this appears to return the same result you're getting:

    Code:
    Function ElapsedTime(Interval) As String
        Dim x
        '  x = Int(CSng(Interval * 24 * 3600)) & " Seconds"
        '  Debug.Print x
        '  x = Int(CSng(Interval * 24 * 60)) & ":" & Format(Interval, "ss") _
           '     & " Minutes:Seconds"
        '  Debug.Print x
        '  x = Int(CSng(Interval * 24)) & ":" & Format(Interval, "nn:ss") _
           '     & " Hours:Minutes:Seconds"
        '  Debug.Print x
        '  x = Int(CSng(Interval)) & " days " & Format(Interval, "hh") _
           '     & " Hours " & Format(Interval, "nn") & " Minutes " & _
           '     Format(Interval, "ss") & " Seconds"
        '  Debug.Print x
        ElapsedTime = Int(CSng(Interval * 24)) & ":" & Format(Interval, "nn:ss")
    End Function
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 07-18-2019, 01:36 AM
  2. Replies: 1
    Last Post: 05-21-2016, 08:51 AM
  3. Replies: 5
    Last Post: 04-11-2015, 03:54 AM
  4. Replies: 42
    Last Post: 03-01-2013, 06:58 AM
  5. Replies: 1
    Last Post: 02-28-2012, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums