Checking for empty fields

  • Thread starter Thread starter Jeff Klein
  • Start date Start date
J

Jeff Klein

I need to have a way to check fields for empty before my form moves to a new
record or closes the form. What is the best event to place the code?
 
This covers all situations:

If Len(Nz(Me!MyField, "")) = 0 then
'Do the blank field processing

TomU
 
Jeff said:
I need to have a way to check fields for empty before my form moves to a new
record or closes the form. What is the best event to place the code?


The Form's BeforeUpdate event.

You can set the event procedure's Cancel argument to True to
prevent the form from moving off the current record until
your checks for missing data pass.
 
Marshall,
Thanks for the reply. I am confused. Is this what you are describing? If
so, this does not stop from proceding to the next record. Maybe I am not
understanding.... wouldn't be the first time.

Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.CancelEvent
End Sub
 
This is the basic code for checking the field

If IsNull(txb_Programs) Then
MsgBox "The Program must be entered", vbOKOnly
txb_Program.SetFocus
DoCmd.CancelEvent
Exit Sub
End If


Jeff Klein said:
Marshall,
Thanks for the reply. I am confused. Is this what you are describing? If
so, this does not stop from proceding to the next record. Maybe I am not
understanding.... wouldn't be the first time.

Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.CancelEvent
End Sub



a
 
Tom, Thanks for the reply. In which event should I use the code? I need
an event that fires before the record is moved. I have been experimenting
with different events...no luck. Any help is appreciated. Jeff
 
Well, that should work, but what I was suggesting was:

If IsNull(txb_Programs) Then
MsgBox "The Program must be entered", vbOKOnly
txb_Program.SetFocus
Cancel = True
Exit Sub
End If

I try to avoid DoCmd whenever there is an alternative,
because it does not allow you to specify which object it is
supposed to operate on. In this case, it probably doesn'e
make a lot of difference.

Try it and see if it does what you want.
 
Back
Top