S
SimonG via AccessMonster.com
I have a function during which a line passes a calculation to an integer,
usually all is fine, but occasionally, where the result of the calculation is
a whole number this is also rounded down (eg 12 is rounded down to 11). Note
this does not happen for all whole numbers passed, the two examples I
currently have are 12 and 28.
I suspect the cause of the problem to be in the phasing of the code for the
calculation, perhaps someone could look-it over and make some suggestions?
Background:
A function is required to round times to the nearest x mins (this element
taken as stand-alone works with no errors yet found), however the function
also needs to allow for an off-set:
eg from 7 o'clock the times to the nearest 15 mins are 7:00; 7:15; 7:30; 7:
45; 8:00 etc.
however from 7:10 (time off set by 10 mins) times are: 7:10; 7:25; 7:40; 7:
55; 8:10 etc
My “simple†solution to this is to remove the off-set time; round the the
nearest x mins; then add back the off-set.
Code:
Public Function TimeRoundDown(dteHighTime As Date, _
Optional intNearest As Integer = 60, _
Optional intMinPast As Integer = 0)
'
' This returns the time Rounded Down to the nearest interval
' if no interval is given time is rounded to nearest hour*.
'
' * mins past is used to round to nearest mins past the hour
' eg time of 7:20, rounding nearest 15 for 10 past the hour
' rounded down time is 7:10
'
Dim intTimeBlks As Double
Dim dblStdMin As Double
Dim dblConv As Double
dblStdMin = intMinPast / (24 * 60)
dblConv = 24 * 60 / nz(intNearest, 60)
dteHighTime = dteHighTime - dblStdMin
' Next line causes error:
intTimeBlks = dteHighTime * dblConv
'
TimeRoundDown = (Int(intTimeBlks) / dblConv) + dblStdMin
End Function
Known Problem values:
3:15 to the nearest 15, off-by 15 mins problem line returns 11 not 12
7:15 to the nearest 15, off-by 15 mins problem line returns 27 not 28
Any suggestions gladly received,
Simon
Ps
Access 2007 on XP or Vista pc.
usually all is fine, but occasionally, where the result of the calculation is
a whole number this is also rounded down (eg 12 is rounded down to 11). Note
this does not happen for all whole numbers passed, the two examples I
currently have are 12 and 28.
I suspect the cause of the problem to be in the phasing of the code for the
calculation, perhaps someone could look-it over and make some suggestions?
Background:
A function is required to round times to the nearest x mins (this element
taken as stand-alone works with no errors yet found), however the function
also needs to allow for an off-set:
eg from 7 o'clock the times to the nearest 15 mins are 7:00; 7:15; 7:30; 7:
45; 8:00 etc.
however from 7:10 (time off set by 10 mins) times are: 7:10; 7:25; 7:40; 7:
55; 8:10 etc
My “simple†solution to this is to remove the off-set time; round the the
nearest x mins; then add back the off-set.
Code:
Public Function TimeRoundDown(dteHighTime As Date, _
Optional intNearest As Integer = 60, _
Optional intMinPast As Integer = 0)
'
' This returns the time Rounded Down to the nearest interval
' if no interval is given time is rounded to nearest hour*.
'
' * mins past is used to round to nearest mins past the hour
' eg time of 7:20, rounding nearest 15 for 10 past the hour
' rounded down time is 7:10
'
Dim intTimeBlks As Double
Dim dblStdMin As Double
Dim dblConv As Double
dblStdMin = intMinPast / (24 * 60)
dblConv = 24 * 60 / nz(intNearest, 60)
dteHighTime = dteHighTime - dblStdMin
' Next line causes error:
intTimeBlks = dteHighTime * dblConv
'
TimeRoundDown = (Int(intTimeBlks) / dblConv) + dblStdMin
End Function
Known Problem values:
3:15 to the nearest 15, off-by 15 mins problem line returns 11 not 12
7:15 to the nearest 15, off-by 15 mins problem line returns 27 not 28
Any suggestions gladly received,
Simon
Ps
Access 2007 on XP or Vista pc.