Validation of date.

J

jfaz

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.
 
O

Ofer Cohen

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
 
D

Douglas J. Steele

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.
 
Top