Testing for Null in Field on form

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

Guest

I am trying to test for a null on a form with the following code. I am
putting it in On Exit.

If isnull(Me.Alt_ID) then
Cancel=True
Msgbox "Field cannot be null!"
Me.Alt_Id.SetFocus
end if

Whenever I test the code, it allows the null, does not come back with any
message, and does not setfocus on the field. I need it to pop up a message
with a null and return to the tested field. In other words, do not let the
user move forward until the field is populated.

Thanks for any help.
 
You are using the wrong event. Your code should be in the control's Before
Update event. In this case, you don't need to set the focus to the control,
the cursor stays in the control.
 
Thanks, Dave.

I suspected that was the problem and probably should have tried it before
posting, but that is what this forum is for, to help people.

Thanks again.
 
Should not the function be used like that:
isnull(Me.Alt_ID.Value), instead of isnull(Me.Alt_ID)?
Regardless which event it is going to...
 
mezzanine1974 said:
Should not the function be used like that:
isnull(Me.Alt_ID.Value), instead of isnull(Me.Alt_ID)?
Regardless which event it is going to...

It is not necessary to specify the .Value property of controls, as it
is the default property. When testing a control, the following
expression will under most circumstances test the same

IsNull(Me.Alt_ID.Value)
IsNull(Me.Alt_ID)
IsNull(Me!Alt_ID.Value)
IsNull(Me!Alt_ID)
IsNull(Me("Alt_ID").Value)
IsNull(Me("Alt_ID"))
IsNull(Me.Controls("Alt_ID").Value)
IsNull(Me.Controls("Alt_ID"))
IsNull(Me.Controls.Item("Alt_ID").Value)
IsNull(Me.Controls.Item("Alt_ID"))

which to use, is mostly about preferences.
 
Back
Top