Check for IsNull

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Null may not be the actual value if the control can't accept a Null value.

Try this:

If Len(Me.Title & "") = 0 Then
 
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
 
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.
 
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
 
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.
 
Back
Top