ACC: No Warning Closing a Form with a Required Field Left NullID: Q180167
|
Moderate: Requires basic macro, coding, and interoperability skills.
When you use code (for example, on a command button) to close a form that
contains a control bound to a field that has its Required property set to
Yes, and you enter no data for that control, you do not receive an error
message.
If you close the same form by using the Windows Close button, the Close
action in a macro, or by clicking Close on the File menu, you do receive
the following message as expected:
The field '<tablename>.<fieldname>' can't contain a null value
because the Required property for this field is set to True.
Enter a value in this field.
-or-
Field '<tablename>.<fieldname>' can't contain a null value.
By default, the changes to the record will be aborted without warning. If
you need to check whether data has been entered in the required field, you
will need to do so manually in code, as shown in the following example:
Table: Table1
------------------
Field Name: Field1
Data Type: Text
Required: Yes
Field Name: Field2
Data Type: Text
Required: No
Name: CloseForm
Caption: Close Form
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
' For Access 2.0 remove the previous line and replace with:
' DoCmd Close
Else
Me![Field1].SetFocus
End If
Else
DoCmd.Close
End If
Table: Table1
------------------
Field Name: Field1
Data Type: Text
Required: Yes
Field Name: Field2
Data Type: Text
Required: No
Name: CloseForm
Caption: Close Form
DoCmd.Close
DoCmd Close
For more information about the Close method, search the Help Index for
"Close Method."
For more information about the Required property, search the Help Index for
"Required Property."
Additional query words: record missing
Keywords : FmsEvnt GnlDe
Version : WINDOWS:2.0,7.0,97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: April 28, 1999