Restrict Entry of Dates in Access 2003

C

cleblanc

I need to not allow a date to be entered into the "Date" field if the
entry date is greater than the 5th day of the current month.

Any help is appreciated.
 
K

Klatuu

If you have a field in a table or a control on a form named Date, change it.
Date is a reserved word. Using reserved words as names will cause problems.
To do what you want, use the Before Update event of the text box where you
enter the date:

Private Sub txtSomeDate_BeforeUpdate(Cancel As Integer)

If Not IsDate(Me.txtSomeDate) Then
MsgBox "Invalid Date Entered", vbExclamation
Cancel = True
ElseIf Day(Me.txtSomeDate) > 5 Then
MsgBox "Must be on or before the 5th of the month", vbExclamation
Cancel = True
End If
End Sub
 
M

missinglinq via AccessMonster.com

"I need to not allow a date to be entered into the "Date" field if the
entry date is greater than the 5th day of the *current* month."

Dave's code only assures that the tstSomeDate is on or before the 5th of
*some* month, not neccessarily the current month. So we need to modify his
code to add validation to assure that the entered date is also the current
month and year:

Private Sub txtSomeDate_BeforeUpdate(Cancel As Integer)

If Not IsDate(Me.txtSomeDate) Then
MsgBox "Invalid Date Entered", vbExclamation
Cancel = True
ElseIf (Month(Me.txtSomeDate) <> Month(Date)) Or (Year(Me.txtSomeDate) <>
Year(Date)) Or (Day(Me.txtSomeDate) > 5) Then
MsgBox "Must be on or before the 5th of the month", vbExclamation
Cancel = True
End If
End Sub
 
M

missinglinq via AccessMonster.com

Just had a stray thought (they come so seldom, I like to nuture them!) Do
your requirements need the entered date to be the 1st thru 5th of the current
month, or can a valid date be any day of any previous month as well?
 
M

missinglinq via AccessMonster.com

If you *do* require the ability to enter enter dates from the 5th of the
current month or EARLIER (i.e. any date before the 5th of the current month,
including dates from previous months) this will do it:

Private Sub txtSomeDate_BeforeUpdate(Cancel As Integer)
If Not IsDate(Me.txtSomeDate) Then
MsgBox "Invalid Date Entered", vbExclamation
Cancel = True
ElseIf Me.txtSomeDate > DateSerial(Year(Date), Month(Date), 5) Then
MsgBox "Must be on or before the 5th of the current month",
vbExclamation
Cancel = True
End If
End Sub
 
Top