Solved

Time Rounding Up Question Access VBA

Posted on 2016-08-26
8
27 Views
Last Modified: 2016-08-27
The following code successfully rounds DOWN my time NOW() to the nearest 5 minutes, but I am struggling to edit it to round UP.

Me.txtStart.Value = CDate((1 * (Int(Now() / (1 * (1 / 288)))) * (1 / 288)))

Open in new window


How can I do this please? I am trying to get away without a function.
0
Comment
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:Computer_Support_Norwich
Comment Utility
Hmmm, is this it?

Me.txtFinish.Value = (CDate(Round(DateAdd("n", 3, Now()) * 288, 0) / 288))

Open in new window


Seems it might be about right.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
This will do:

Dim datTime As Date

datTime = Now

Me!txtFinish.Value = DateValue(datTime) + TimeSerial(Hour(datTime), -Int(-Minute(datTime) / 5) * 5, 0)

Open in new window

/gustav
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
I am trying to get away without a function.
Why?
And /gustav's might be good too.
I didn't test it

Here's mine

Me.txtStart.Value = CDate(Int(Now) + (Int((CDbl(Now) - Int(Now)) * 24 * 12) + 1) / (24 * 12))
Here's the logic
We're going to coerce the date to a Double and remove the day part
CDbl(Now) - Int(Now)
That leaves us with the fraction of the day
There's 24 hours in a day and 12 five minute periods in an hour, so we'll change the fraction into 5 minute periods
CDbl(Now) - Int(Now) *24*12
Now we'll truncate that to get rid of fractional sub 5 minute amounts
Int(CDbl(Now) - Int(Now) *24*12)
Then add one to round up
Int(CDbl(Now) - Int(Now) *24*12) + 1
Convert it back to a fraction of a day
(Int(CDbl(Now) - Int(Now) *24*12) + 1)/(24*12)
Add the date whole number back on
Int(Now) + (Int(CDbl(Now) - Int(Now) *24*12) + 1)/(24*12)
And coerce it to a date
CDate(Int(Now) + (Int(CDbl(Now) - Int(Now) *24*12) + 1)/(24*12))

But a function would make for much more maintainable code!
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
gustav's is always good, as he is the Master of all things date, time and rounding :-)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 26

Expert Comment

by:Nick67
Comment Utility
<grin>
I've remarked on that and suggested an Article should be written
Alas!  It hasn't been forthcoming.

But the ask was for 'not a function'
You can turn /gustav's into 'not a function' but it doesn't start that way.

It's been a while since I've played with Hijri dates.
Those were interesting questions.
Depending on many things, a day can be the last day of the month in one country and the first day of the month in another.
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
I've remarked on that and suggested an Article should be written
Alas!  It hasn't been forthcoming.
I know and remember just too well, and I'm about it - turns out as a major task.

But the ask was for 'not a function'
That's why I didn't write a function ... just inserted two lines before the existing code line.

/gustav
0
 

Author Closing Comment

by:Computer_Support_Norwich
Comment Utility
Thank you Gustav worked first time.

Why do I want to get away without a function? It's just easier for me in this situation.
0
 

Author Comment

by:Computer_Support_Norwich
Comment Utility
Nick thank you too :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now