I have a date field on a form that must only be a sunday date. I want to
attach a macro that will show a message box if the date entered is not a
Sunday. I have tried several ways to achieve this but not been sucessful.
Any help would be gratefully appreciated.
Ofer Cohen - 17 Jul 2007 12:00 GMT
In the BeforeUpdate event of the date text box use the following code to
validate the date
If WeekDay(Me.[TextBoxName]) <> 1 Then
MsgBox "Date must be Sunday"
Cancel = True ' will stop the exit from the field
End If
If you just want to display a message, without stoping the process, then
remove
Cancel = True
from the code

Signature
Good Luck
BS"D
> I have a date field on a form that must only be a sunday date. I want to
> attach a macro that will show a message box if the date entered is not a
> Sunday. I have tried several ways to achieve this but not been sucessful.
> Any help would be gratefully appreciated.
Douglas J. Steele - 17 Jul 2007 12:02 GMT
Assuming this is a bound field (named, for the sake of argument,
txtDateField), try something like:
Private Sub txtDateField_BeforeUpdate(Cancel As Integer)
If IsDate(Me!txtDateField) Then
If Weekday(Me!txtDateField) <> vbSunday Then
MsgBox "The date must be a Sunday"
Cancel = True
End If
Else
MsgBox "You must provide a date"
Cancel = True
End If
End Sub
On the other hand, you could simply change the date to the closest Sunday.

Signature
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
>I have a date field on a form that must only be a sunday date. I want to
> attach a macro that will show a message box if the date entered is not a
> Sunday. I have tried several ways to achieve this but not been sucessful.
> Any help would be gratefully appreciated.