Hours Can Only Increment By 15 Minutes

burrina

Registered User.
Local time
Today, 09:23
Joined
May 10, 2014
Messages
972
For Example: I have a field, let's say called Hours set to Data Type Number and Double format.

The user should only be able to enter time in 1/4 hr increments.
1 hr or 1.25 or 1.5 etc...
 
You can round a time to the nearest 15 minutes. 15 minutes is . . .
Code:
Function Get15Minutes As Double
[COLOR="Green"]    '1 minute is one day / 24 hours / 60 minutes
    'so 15 minutes is 15 / 24 / 60
[/COLOR]    Get15minutes = 15 / 24 / 60
End Function
. . . and to round to the nearest x, you divide by x, truncate, and muliply by x
Code:
Function RoundToNearest(Number as double, RoundTo as double) as double
   RoundToNearest = RoundTo * Int(Number / RoundTo)
End Function
So to round a date/time to the nearest 15 minutes . . .
Code:
Debug.Print RoundToNearest(Now(), Get15Minutes)
Does that make sense? And just do that to the date AfterUpdate().
 
So, would I call that function using the before update event or call it from a module ?
This is the one for me.
Function Get15Minutes As Double
'1 minute is one day / 24 hours / 60 minutes
'so 15 minutes is 15 / 24 / 60
Get15minutes = 15 / 24 / 60
End Function
 
If I had to constrain user input to round a date/time to 15 minute increments I would handle AfterUpdate, and just change the data . . .
Code:
private sub SomeDateTime_AfterUpdate()
   If IsDate(Me.SomeDateTime) then
      Me.SomeDateTime = RoundToNearest(Me.SomeDateTime, 15 / 24 / 60)
   Else
      msgbox "Date is not valid!"
      Me.SomeDateTime = null
   end if
end sub
 
It's not a Date though, it's a Number, i.e. 1 or 1.25 or 1.5 or 1.75
 
burrina,

Just for clarity. Is the issue one of time differences. That is, something was started at time X and finished at time Y, and you want to record that time difference Y-X to the nearest 15 minutes (or 1/4 hour). Is that the objective?
 
NO, it's simply a matter of a Numeric expression. I want to limit the value to 1/4 hrs.
I can use of course a IsNotNumeric but also need to limit it to 1/4 hr values.

Example: 1 hour, 1.25 hours, 1.5 hours, 1.75 hours , etc...
It's not a date from or hour to value, it's 1 single value.
 
So you have a number you want to round to the nearest 0.25?
Code:
private sub SomeNumber_AfterUpdate()
   If IsNumeric(Me.SomeNumber) then
      Me.SomeNumber = RoundToNearest(Me.SomeNumber, [B][COLOR="DarkRed"]0.25[/COLOR][/B])
   Else
      msgbox "Number is not numeric!"
      Me.SomeNumber = null
   end if
end sub
 
Thank You, That's exactly what I needed.

Thank You Mark and jdraw.
 

Users who are viewing this thread

Back
Top Bottom