Error on empty required field

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

Guest

I must be majorly missing something. I have a field on a form named StatDate
which is a required date field. If I try to close the form and the field is
null I get the standard error message. I tried using the code supplied in the
Access help "Close Method" but I still do not get the msg box the code
builds.

Do I need this process as a new private function? Do I put it in the
Form_Close section?

If possible, I would like to see a completed code example.
Thanks.
 
how are you closing the form: by clicking a command button? by clicking the
"X" button at the right side of the form's title bar? or...?
you said you tried using the code from Help - where did you run it from?
 
Tina,

I am closing the form using the "X" close button on the title bar. On
Access' VBA window's help for "Close Form" it addresses the required field
problem. I copied the code into an expression in the Close_Form() and used
the form's StatDate field. Below is the code that was provided.. (When I get
the error messaged when closing the form, it does not run this code.)

If IsNull(Me![Field1]) Then
If MsgBox("'Field1' must contain a value." _
& Chr(13) & Chr(10) _
& "Press 'OK' to return and enter a value." _
& Chr(13) & Chr(10) _
& "Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = _
vbCancel Then
DoCmd.Close
End If
End If
 
the Close Method applies to the DoCmd object. normally you use this method
to close the form from a command button or some other control or event on
your form, as

DoCmd.Close

the Form_Close event runs before the form closes, but *after* you tell the
form to close with a DoCmd.Close or by clicking the "X" button on the title
bar, etc. so putting a Close instruction in that event is redundant. also,
the Form_Close event cannot be cancelled, so it does no good to put any kind
of validation code there.

i couldn't find a way to cancel the Close action when the "X" button is
clicked. suggest you remove it from your form, and place a command button on
the form to close it. in the command button's Click event, you can use the
code you found in Help. but it needs some tweaking because, as written: 1)
if the required value is *not* missing, nothing happens - the form does not
close, and 2) the code will check for a value in the required field *even
if* no record was entered - in other words, if the user opens the form but
decides to close it without adding a record, the message box will still run.
try this code instead, as

Private Sub cmdClose_Click()

If Me.Dirty And IsNull(Me!StatDate) Then
If MsgBox("'StatDate' must contain a value." _
& vbCr & "Press 'OK' to return and enter a value." _
& vbCr & "Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = vbOK Then
Me!StatDate.SetFocus
Exit Sub
End If
End If

DoCmd.Close , , acSaveNo

End Sub

change "cmdClose" to the name of your command button, of course.

hth


Wylie C said:
Tina,

I am closing the form using the "X" close button on the title bar. On
Access' VBA window's help for "Close Form" it addresses the required field
problem. I copied the code into an expression in the Close_Form() and used
the form's StatDate field. Below is the code that was provided.. (When I get
the error messaged when closing the form, it does not run this code.)

If IsNull(Me![Field1]) Then
If MsgBox("'Field1' must contain a value." _
& Chr(13) & Chr(10) _
& "Press 'OK' to return and enter a value." _
& Chr(13) & Chr(10) _
& "Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = _
vbCancel Then
DoCmd.Close
End If
End If


tina said:
how are you closing the form: by clicking a command button? by clicking the
"X" button at the right side of the form's title bar? or...?
you said you tried using the code from Help - where did you run it from?


field
is in
the
 
Back
Top