Focus

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I put in a control for the [Date] field in a form which
will help eliminate data entry errors. Here is my code:

Private Sub Date_Exit(Cancel As Integer)
If (([Date] > (Now() + 7)) Or ([Date] < (Now() - 7))) Then
MsgBox "Invalid Date - Try Again!", vbOKOnly
Me!Date.SetFocus
End If
End Sub

The problem is that the focus goes to the next field, not
the [Date] field.

Does anyone see the problem?

Thanks for the help.
 
Steve said:
I put in a control for the [Date] field in a form which
will help eliminate data entry errors. Here is my code:

Private Sub Date_Exit(Cancel As Integer)
If (([Date] > (Now() + 7)) Or ([Date] < (Now() - 7))) Then
MsgBox "Invalid Date - Try Again!", vbOKOnly
Me!Date.SetFocus
End If
End Sub

The problem is that the focus goes to the next field, not
the [Date] field.

Does anyone see the problem?

Thanks for the help.

Don't use the Exit event for this kind of validation. Use the control's
BeforeUpdate event, and set the Cancel argument to True to keep the
focus in the control. Also, don't name the control or the field "Date",
unless you want to continually struggle with making Access understand
that you mean the field/control and not the VBA Date function.

Supposing that you had named your control "DueDate" -- bearing in mind
that I have no idea what the meaning of this particular date field is --
you could have a BeforeUpdate event procedure like this:

Private Sub DueDate_BeforeUpdate(Cancel As Integer)
If (Me.DueDate > (Date() + 7)) _
Or (Me.DueDate < (Date() - 7)) _
Then
MsgBox "Invalid Date - Try Again!", vbOKOnly
Cancel = True
End If
End Sub
 
This helps a lot. One problem though, after the message
box comes up an additional error message appears. I
don't want it to appear and changed the code to the
following but it still appears. Do you know how to fix
this?

Thanks
 
Steve said:
This helps a lot. One problem though, after the message
box comes up an additional error message appears. I
don't want it to appear and changed the code to the
following but it still appears. Do you know how to fix
this?

Thanks

What message? What "following"?
 
Back
Top