Data required in field

  • Thread starter Thread starter Walter Maas
  • Start date Start date
W

Walter Maas

I have entered the following code in the On Exit property of a date
field on my form to warn users that they have to enter a date in the
field and the code also makes the focus to stay on the field until data
has been entered:

Private Sub Date_Event_Exit(Cancel As Integer)
If IsNull([Date Event]) Then
DoCmd.CancelEvent
MsgBox "You must enter the date"
Else
End If

End Sub

This shows a message when the <Date Event> field has not received any
data and the focus will stay in the field until some date has been
entered.
However the message also comes up when I close the form without having
done anything (i.e. I just open the form and close it again). It would
be better if the message only came up when I exit the field (with TAB
or mouse). Also the message comes up several times (also after the form
has closed) and you have to click it away several times. This is
annoying.

The Date Event field in the table is set to Required but this doesn't
help in the form, it will accept an empty field(?)
I have tried the Validation setting Is Not Null, but then the user can
complete the whole record and only then it will say that the date has
to be entered. It would like it to happen when they want to leave the
date field and before they complete all the other fields in the
record.
Can anyone help me?
 
Try using your code in the BeforeUpdate event of the form.

Code
-------------------

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull([Date Event]) Then
MsgBox "You must enter the date"
DoCmd.CancelEvent
Else
'do nothing
End If

End Sub
 
Thanks THT but this doesn't do anything at all (nothing happens and
can still go to the next field in the record).

Walte
 
Do you want to post a stripped down version of your db and I'll take
look for you. It is better to place code in the BeforeUpdate event o
the form. You can't really use the Exit or AfterUpdate events o
controls to trigger your editing. If the cursor is never placed in
control, its Exit event will never fire. When you place edit code i
the BeforeUpdate event of the form, you must cancel the update event i
any errors are found.
 
Back
Top