Validation of date.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

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

Excel Excel Show Countdown Date 7
Validation in child form 1
Excel Help with date adding formulas! 7
Access Property sheet data missing also syntax 0
validate date entered 3
main form - subform 5
Restrict a day 3
Specific Date 3

Back
Top