Control of negative time expression formatting (1 Viewer)

matt beamish

Registered User.
Local time
Today, 20:45
Joined
Sep 21, 2000
Messages
208
Hello folks.
I am using a little explanation posted by Allen Browne (here http://allenbrowne.com/casu-13.html on expressing minutes as hours and minutes, and I have written this little function:

Code:
Function Calc_HrsMins(TotalMinutes As Variant) As Variant
On Error GoTo calc_hrsmins_error
Dim varHours, varMinutes
varHours = Int(TotalMinutes \ 60)
varMinutes = Format([TotalMinutes] Mod 60, "\:00")
Calc_HrsMins = varHours & varMinutes

Exit_Calc_HrsMins:
   Exit Function

calc_hrsmins_error:
MsgBox Err.Number & Err.Description
Exit Function

End Function

if my value is negative, I get these results:
TotalMinutes = -500, -8-:20
TotalMinutes = -5, 0-:05

I would like to suppress the negative symbol for the minutes and always show it for the hours, even if the hours are 0, so text boxes display -8:20 and -0:05 respectively.

I think this is going to be by using a series of IIfs, but I dont know how best to suppress the negative...multiply it by -1?

Any advice please?

thanks in advance
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:45
Joined
Feb 19, 2013
Messages
16,633
suggest try using the abs function

varMinutes = Format(abs([TotalMinutes]) Mod 60, "\:00")
 

matt beamish

Registered User.
Local time
Today, 20:45
Joined
Sep 21, 2000
Messages
208
Thanks very much for your reply, I will give this a try tonight.
Matt
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:45
Joined
May 7, 2009
Messages
19,247
varHours = Abs(Int(TotalMinutes \ 60))
varMinutes = Format([TotalMinutes] Mod 60, "\:00;\:00")
 

matt beamish

Registered User.
Local time
Today, 20:45
Joined
Sep 21, 2000
Messages
208
To get what I needed, to suppress the double negative symbol but to show a negative if the hours = 0 and minutes = < 0 I ended up with this:

Code:
Option Compare Database
Function Calc_HrsMins(TotalMinutes As Variant) As Variant
On Error GoTo calc_hrsmins_error
Dim varHours, varMinutes

varHours = IIf(Int(TotalMinutes \ 60) = 0 And (TotalMinutes Mod 60 < 0), Format(Int(TotalMinutes \ 60), "-0"), Int(TotalMinutes \ 60))
varMinutes = Format(Abs([TotalMinutes]) Mod 60, "\:00")


Calc_HrsMins = varHours & varMinutes

Exit_Calc_HrsMins:
   Exit Function

calc_hrsmins_error:
MsgBox Err.Number & Err.Description
Exit Function

End Function
 

MarionD

Registered User.
Local time
Today, 20:45
Joined
Oct 10, 2000
Messages
421
Hi there, don't know if till relevant but I found an easy workabout:

If guthabenrecs!Endwert > 0 Then
Me.guthaben_std = (Me.Guthaben * 1440) \ 60 & Format(Me.Guthaben * 1440 Mod 60, "\:00")
Else
Me.guthaben_std = (Me.Guthaben * 1440) \ 60 & Format((0 - Me.Guthaben) * 1440 Mod 60, "\:00")
Me.guthaben_std.ForeColor = 255
End If


Just putting a 0 - theAmount in the minutes section

M
 

Users who are viewing this thread

Top Bottom