Always future dates

S

Scott

I have a form with an unbound text box for entering a date. The text
box's format property is set to Short Date. Here's the problem, based
on today (09/14/07):
If a user enters 01/15 into the text box, Access will expand that date
to 01/15/07. The users would like the date to be expanded to 01/15/08,
the future date instead of one in the past.

Can someone suggest the easiest way to force the date into the future?
Is there some sort of formatting property I can set, or would this
need to be done in code? If it's in code, have you seen a sample
anywhere that would give me a good starting point?

Thanks!
 
A

Allen Browne

Use the AfterUpdate event procedure of the text box to add one year if the
date is already past.

This kind of thing (substituting your text box name for Text0):

Private Sub Text0_AfterUpate
If Me.Text0 < Date() Then
Me.Text0 = DateAdd("yyyy", 1, Me.Text0)
End If
End Sub
 
S

Scott

Thanks for the response, Allen. Your comments and your site have
always helped me out and are a real value to the community.

Your suggestion is on target, but I was worried about about one
problem I didn't list: sometimes the users will legitimately need to
enter a past date. As a result, I need to only move the date when a
user enters a short date like:
m/d, mm/d, mm/dd, or m/dd (or the same, except with a space instead of
a slash). These are the cases that Access expands out to a full date
in the current year.

To solve this, I've taken an example from your site (which is great!)
and customized it. I've modified the items from http://allenbrowne.com/ser-34.html
into this subroutine that moves dates. Hopefully it helps someone in
the future. Allen, feel free to comment if you see something that
could be improved... especially if you know of a way to catch the
value of a bound field before a mouse click (see below).

Thanks again,

Scott
------------------------------------------------------------
Sub subFutureDates(ctl As Control, Optional KeyAscii As Integer)

' Purpose: If a user enters a date without a year, always set
that to a future date.
' These dates can be: m/d, mm/d, mm/dd, or m/dd (or
the same, except with a space instead of a slash).
' Usage: Call the subroutine from the control's KeyDown event
procedure:
' Call subFutureDates(Me.MyTextBox, KeyAscii)
' And call the subroutine from the control's KeyDown
event procedure, omitting the optional KeyAscii
' Call subFutureDates(Me.MyTextBox)
'
' NOTE: You must put this in the KeyDown event, NOT
KeyPress
' KeyPress will not capture return or tab keys
' (See MSDN article titled, "Order of events
for keystrokes and mouse clicks")
'
' NOTE: If you have a control bound to a date field, you can
catch users exiting the field using a
' tab or enter keystroke, but you WILL NOT be able to
catch a mouse click before the date changes.
' Access modifies the date as soon as a user clicks out
of the field, before any events fire
' So, by the time you can grab the date you won't be
able to tell what was entered.
' One way to work around this: make your field unbound,
then copy the value after you clean it up.
'
' Error handling: add your own as desired!

Dim dteDateValue As Date

'KeyAscii is optional in order to allow us to call this from
'a control's exit event

'We only act if the user hits tab or enter or if KeyAscii is null
If (KeyAscii = Asc(vbTab)) Or (KeyAscii = 13) Or (KeyAscii = 0)
Then

'To avoid an error, we'll set the variable to today
dteDateValue = date

'First, check if the entry is either large enough to be a full
date
'or is too small to be a valid date
'If it is either, then then we'll leave it and exit
'The smallest possible full date is like 1/1/1, so five
characters
'The smallest possible month/date combo is three characters,
like 1/1

If (Len(ctl.Text) >= 5) Or (Len(ctl.Text) < 3) Then Exit Sub

'OK.. we have a partial date
'Check if the date would be before today
'To keep this simple, I'm NOT validating that this value can
be a date
'Instead, we'll just resume next...
On Error Resume Next
dteDateValue = CDate(ctl.Text)
On Error GoTo 0

'Now, if the date is less than today, then we'll move it...
If dteDateValue < date Then

'The date is before today, so let's move it to next year
dteDateValue = DateAdd("yyyy", 1, CDate(ctl.Text))
ctl.Text = dteDateValue

End If

End If

Exit Sub
 
A

Allen Browne

Fair enough. It's always good to see someone taking the suggestions further
and creating their own solutions. Thanks for posting so others can benefit.

If you need to know whether the user entered just something such as 3/5
rather than including the year, you can examine the Text property of the
control in its AfterUpdate event procedure. While the Value will always
return a full date, the Text will show you what's actually visible there at
the time. This lets you take the action you need.

This code for dividing a value by 100 if the user did not actually type the
percent sign takes a similiar approach:
http://allenbrowne.com/casu-16.html

All the best
 

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

Similar Threads

Not allow future dates 1
DateAdd problem 2
Getting a nice layout 1
counting based on two dates 2
Access Count dates within a Month 4
Find then highlight in yellow 6
Help me about Filter 3
Limit table 6

Top