I have copied the following code from another thread, post #4, the thread is located here:
It is not working , please help
roundup or down to the nearest half is it possible?
Hi I'm wandering if it's possible to get access to round numbers to the nearest 1/2 hour i.e. 0.5, 1.5 or 2.0 depending on the result. Any help much appreciated. Oh by the way I'm using 2003.
www.access-programmers.co.uk
It is not working , please help
Code:
Public Function RoundTime(varTime As Variant, Optional ByVal lngSeconds As Long = 900&) As Variant
'The function below rounds a date/time value to the specified number of seconds. For example, to round to the nearest half hour (30 * 60 seconds), use:
' =RoundTime([MyDateTimeField], 1800)
'Purpose: Round a date/time value to the nearest number of seconds
'Arguments: varTime = the date/time value
' lngSeconds = number of seconds to round to.
' e.g. 60 for nearest minute,
' 600 for nearest 10 minutes,
' 3600 for nearest hour,
' 86400 for nearest day.
'Return: Rounded date/time value, or Null if no date/time passed in.
'Note: lngSeconds must be between 1 and 86400.
' Default rounds is nearest 15 minutes.
Dim lngSecondsOffset As Long
RoundTime = Null 'Initialize to return Null.
If Not IsError(varTime) Then
If IsDate(varTime) Then
If (lngSeconds < 1&) Or (lngSeconds > 86400) Then
lngSeconds = 1&
End If
lngSecondsOffset = lngSeconds * CLng(DateDiff("s", #12:00:00 AM#, TimeValue(varTime)) / lngSeconds)
RoundTime = DateAdd("s", lngSecondsOffset, DateValue(varTime))
End If
End If
End Function