Required Field

G

Guest

I have a field on my form that is required for all new records created. This
is the code I have in the BeforeUpdate event on my form.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.RequestedBy) Then

MsgBox "You Must Select Your Name!"

Cancel = True

Me.RequestedBy.SetFocus

End If

End Sub

It doesn't seem to be working properly. Right now if I created a new record
and didn't fill in that field and just clicked the close button on my form I
do get the msgbox but then the form closes. Am I missing something here? It
doesn't set the focus on that field the way it's written in the code. I also
have this field flagged as required in the table. What am I doing wrong?
 
J

John W. Vinson

I have a field on my form that is required for all new records created. This
is the code I have in the BeforeUpdate event on my form.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.RequestedBy) Then

MsgBox "You Must Select Your Name!"

Cancel = True

Me.RequestedBy.SetFocus

End If

End Sub

It doesn't seem to be working properly. Right now if I created a new record
and didn't fill in that field and just clicked the close button on my form I
do get the msgbox but then the form closes. Am I missing something here? It
doesn't set the focus on that field the way it's written in the code. I also
have this field flagged as required in the table. What am I doing wrong?

Did you actually *create a new record* - by dirtying some *other* field? If
not, then the BeforeUpdate event will not fire, because there's nothing to
update. You're just opening a blank, uninitialized record and then closing it.

If you did in fact enter data into some other field, then there's clearly
something wrong - more details please!

John W. Vinson [MVP]
 

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

Required data in a form 1
Else without If 3
Form_beforeUpdate question 5
On Exit Event 2
VB Coding Assistance 2
DoCmd.GoToControl 10
Require Field Entry based On Other Entry 2
Force Validation prior to leaving form. 3

Top