Check for IsNull

G

Guest

I am trying to check controls for "IsNull" on the Control's BeforeUpdate, but
the system seems to simply blow by my logic.

Here is my code:

Private Sub Title_BeforeUpdate(Cancel As Integer)

If IsNull(Me.Title) Or Me.Title = "" Then
MsgBox ("Title Field is required. Please Enter Data.")
Set CtrlPrevious1 = Me.Title
End If

End Sub

Thanks,
Leslie
 
K

Ken Snell [MVP]

Null may not be the actual value if the control can't accept a Null value.

Try this:

If Len(Me.Title & "") = 0 Then
 
K

Ken Snell [MVP]

Also, you're not cancelling the event in your code:

If Len(Me.Title & "") = 0 Then
MsgBox ("Title Field is required. Please Enter Data.")
Cancel = True
Set CtrlPrevious1 = Me.Title
End If
 
D

Dirk Goldgar

Leslie said:
I am trying to check controls for "IsNull" on the Control's
BeforeUpdate, but the system seems to simply blow by my logic.

Here is my code:

Private Sub Title_BeforeUpdate(Cancel As Integer)

If IsNull(Me.Title) Or Me.Title = "" Then
MsgBox ("Title Field is required. Please Enter Data.")
Set CtrlPrevious1 = Me.Title
End If

End Sub

Thanks,
Leslie

The problem may be that you aren't canceling the update. Are you
getting the message, but having the focus move on anyway? Add the line

Cancel = True

inside your If block.
 
G

Guest

Dirk and Ken,

Thank you for the hints -- I will give this a try. Can you explain how the
"Cancel" is being used in this case? I think I need a class on VB to get
some of these inuendos.

Thanks!
Leslie
 
D

Dirk Goldgar

Leslie said:
Dirk and Ken,

Thank you for the hints -- I will give this a try. Can you explain
how the "Cancel" is being used in this case? I think I need a class
on VB to get some of these inuendos.

:

The problem may be that you aren't canceling the update. Are you

The BeforeUpdate event can be cancelled, which prevents the update from
occurring and returns the focus to the control being updated. The event
procedure for this event has a Cancel argument:

Private Sub Title_BeforeUpdate(Cancel As Integer)

which you can set to True (or any non-zero value) to cancel the event.
That's what the line Ken and I suggested does: it cancels the event,
and thereby cancels the upate of the control.

If you don't set that argument (or call DoCmd.CancelEvent, which
accomplishes the same thing), then the update will take place no matter
what else you do short of turning off your computer. If you do cancel
the event, then the control won't be updated.
 

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

Top