Friday, February 24, 2012

Custom Code/function to format Seconds to hh:mm:ss with ability to go over 24 hours

Hello,

I am trying to get this to work - but it only returns minutes & seconds:

Function Seconds2mmss(ByVal seconds As Integer) As String
Dim ss As Integer = seconds Mod 60
Dim mm As Integer = (seconds - ss) / 60
Seconds2mmss = String.Format("{0:0}:{1:00}", mm, ss)
End Function

Can anyone help me out? I am not that familiar with VB.

Thanks,

Deb

I think this is more of an access report expression format, but it should help you out.

=Int([sumofacd]/3600) & ":" & (Int([sumofacd]/60)-(Int([sumofacd]/3600)*60)) & ":" & format(([sumofacd] Mod 60),"00")

"It works by dividing sumofacd by the number of seconds in 1 hour, the integer of this then becomes the Hours, we then need to take the remainder and convert it to Minutes.

There is more than one way to do the next part. My choice is to divide sumofacd by 60, the integer of this will be the total number of minutes from sumofacd, I then repeat the first part of the equation this time multiplying it by 60 to change it back to minutes, I then minus this from the minutes calculated earlier, this gives the minutes (ie less than 60 or 1 hour).

To get seconds is much easier, as the other elements are made up of multiples of 60 we only require the balance, below 60 so we use the VB Mod function to do this and then format the result.

My first error was to ignore the formatting, doing this would give a :3 instead of :03, and thinking about it you maybe should format the minutes the same way to give 0:03:03 instead of 0:3:03.

Think I will stop looking at the equation, can see additional brakets in the minutes calculation now, for the record would complete each part of the equation before attempting to use the format, as you need the result before formatting, this way you are less likely to get mixed up due to the high number of brackets used"

http://www.utteraccess.com/forums/showflat.php?Board=87&Number=1140884

|||

Thanks for your reply, however, I am not sure how to incorporate this into my report. Does the above expression belong in the code itself, or is it referenced in a report field? This is the first time I've ever tried to use code in a RS report, if you could steer me in the right direction I'm sure I can figure it out.

Deb

No comments:

Post a Comment