Default date based on current time

K

Kevin Mc.

Is it possibe to set the default date in a form based on the time of day?
For example:
Between midnight and 6:00 PM - default to current date.
Between 6:01 PM and 12:59PM - default to current date+1.
Any advise is appreciated.
 
A

Arvin Meyer [MVP]

Sure, let's assume that you want the actual time on the computer. Create a
function (air code) in a standard module, so you can easily call it from
anywhere:

Public Function SetDate() As Date
If Time() <= 0.75 Then
SetDate = Date
Else
SetDate = Date + 1
End If
End Function

Now just use the function in your form's date field as the default value

= SetDate()
 
R

Rick Brandt

Is it possibe to set the default date in a form based on the time of
day? For example:
Between midnight and 6:00 PM - default to current date. Between 6:01 PM
and 12:59PM - default to current date+1. Any advise is appreciated.

Use a DefaultValue like...

=DateValue(DateAdd("h", 6, Now()))

Tweak as testing dictates.
 
T

Tom van Stiphout

On Sun, 11 Jan 2009 08:04:00 -0800, Kevin Mc.

Sure. In the Form_Load event write:
If Time < #6:00:00 AM# Then
Me.MyTimeControl = Date
Else
Me.MyTimeControl = DateAdd("d", 1, Date)
End If

-Tom.
Microsoft Access MVP
 
D

Douglas J. Steele

Not as a default value, but through code in the form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.NewRecord Then
If Time() <= #18:00:00# Then
Me!MyDateField = Date()
Else
Me!MyDateField = DateAdd("d", 1, Date())
End If
End If

End Sub

I suppose you could also put the code in the form's Current event instead if
you wanted the value to appear on the form as you created the new entry.
 
T

tina

nice, Arvin, so clean and simple! i keep forgetting that Date/Time is a
number value, and that it's sometimes easier to manipulate it from that
angle. when i read code like this, it turns that lightbulb on - now if only
i could stop turning it back off! ;)
 
T

Tom van Stiphout

What you consider "nice" I consider using dangerous programming
practice. Especially in the light of functions like DateAdd and
DateDiff designed specifically for date manipulations. Trusting the
underlying data format (and if you want to do that, why not REALLY
read or manipulate the 8-byte data format directly) is just not a safe
practice.

-Tom.
Microsoft Access MVP
 
T

tina

ah, so you got the concept engrained in you, before software enhancements
came along to soften up the ground for us newer folk. this younger
generation - we just don't know how easy we have it... <g>


Arvin Meyer said:
Thanks Tina. I've been working with Access since the day after it was
introduced. Back then in the 16 bit days of Access 1.0 through 2.0, there
was no Date/Time data type. We just used a Double.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
 
A

Arvin Meyer [MVP]

Considering that the Time() function has been around since VB became a
language, and the Date/Time data structure has been around since Microsoft
began writing code. And every language that Microsoft produces uses the same
Date/Time data structure, I doubt that there's any real danger. Also
considering that the same data structure underlie the language
interpretations that are compiled, any change would affect DateAdd and
DateDiff as well.

That said, there's nothing wrong with using DateAdd or DateDiff
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Tom van Stiphout said:
What you consider "nice" I consider using dangerous programming
practice. Especially in the light of functions like DateAdd and
DateDiff designed specifically for date manipulations. Trusting the
underlying data format (and if you want to do that, why not REALLY
read or manipulate the 8-byte data format directly) is just not a safe
practice.

-Tom.
Microsoft Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top