W
William Wisnieski
Hello Everyone,
Access 2000
I'm trying to set up data validation code for a date field on a form. I've
put the code below in the AfterUpdate event of the [TADate] field. If the
user enters a date with a year other than the current year, I'd like the
user to be prompted with a question that asks, "do you want to use that
year"? If they answer yes, then the value is accepted and the cursor moves
to the next field [EmployeeID]. If they answer no, the value is not
accepted and the user must enter the current year. I've tried the following
code, but it doesn't work for two reasons: 1) it doesn't recognize when the
year entered is greater than or less than the current year and 2) if the
user selects no, it still accepts the value and sends them to the next field
[EmployeeID] anyway. I also tried putting it on the BeforeUpdate event of
the field but that wouldn't work because I kept getting an error message
saying I couldn't use SetFocus without saving the field. Here's the code:
Private Sub TADate_AfterUpdate()
If Year(Me.TADate) <> Now Then
Dim Msg, Style, Title, Response
Msg = "The year entered is not current. Do you want to use that year?" '
Define message.
Style = vbYesNo + vbCritical ' Define buttons.
Title = "Year Not Current" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes. Move to Next Field.
Me.EmployeeID.SetFocus
Else ' User chose No. Cancel Event and remain on Current Field.
DoCmd.CancelEvent
End If
End If
End Sub
Any help is much appreciated.
William
Access 2000
I'm trying to set up data validation code for a date field on a form. I've
put the code below in the AfterUpdate event of the [TADate] field. If the
user enters a date with a year other than the current year, I'd like the
user to be prompted with a question that asks, "do you want to use that
year"? If they answer yes, then the value is accepted and the cursor moves
to the next field [EmployeeID]. If they answer no, the value is not
accepted and the user must enter the current year. I've tried the following
code, but it doesn't work for two reasons: 1) it doesn't recognize when the
year entered is greater than or less than the current year and 2) if the
user selects no, it still accepts the value and sends them to the next field
[EmployeeID] anyway. I also tried putting it on the BeforeUpdate event of
the field but that wouldn't work because I kept getting an error message
saying I couldn't use SetFocus without saving the field. Here's the code:
Private Sub TADate_AfterUpdate()
If Year(Me.TADate) <> Now Then
Dim Msg, Style, Title, Response
Msg = "The year entered is not current. Do you want to use that year?" '
Define message.
Style = vbYesNo + vbCritical ' Define buttons.
Title = "Year Not Current" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes. Move to Next Field.
Me.EmployeeID.SetFocus
Else ' User chose No. Cancel Event and remain on Current Field.
DoCmd.CancelEvent
End If
End If
End Sub
Any help is much appreciated.
William