Re: required fields
- From: "Allen Browne" <AllenBrowne@xxxxxxxxxxxxxx>
- Date: Fri, 26 May 2006 00:33:19 +0800
Settting the Required property of each field in your table should be enough
to prevent Access saving the record if the field is null.
If you attempt to close the form while some required fields are left blank,
you should get a message indicating that the record cannot be saved and
asking whether to continue closing (and lose the entry), or complete the
entry before closing. At least, that's what happens if you click the close
button on the right-end of the form's title bar; if you use the Close
action/method in a macro/code, Access fails to give this message. For a
workaround, see:
http://allenbrowne.com/bug-01.html
You can also use the BeforeUpdate event if you wish. Make sure you are using
the BeforeUpdate of the *form*, not of the controls: the control's events
won't work, because they don't fire if nothing is entered.
If you did use Form_BeforeUpdate, but you using DoCmd.Close (or the close
action in a macro), Access does close the form and lose the entry. This is
the same bug discussed in the article link above, and the solution is the
same (i.e. explicitly save before using Close.)
It might be nicer to give the user a single box explaining what is wrong
with the record instead of a whole series. You can do that like this:
Private Sub Form_BeforeUpate(Cancel As Integer)
Dim strMsg As String
If IsNull(Me.Chain_Suffix) Then
strMsg = strMsg & "Please select a Chain Suffix" & vbCrLf
Cancel = True
End If
If IsNull(Me.Region_Code) Then
strMsg = strMsg & "Please select a Region" & vbCrLf
Cancel = True
End If
If strMsg <> vbNullString Then
MsgBox strMsg, vbExclamation, "Invalid entry"
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Lori" <Lori@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:FF9F4B36-F7E5-4A4F-ADDB-8507199D7292@xxxxxxxxxxxxxxxx
I have a form called frm_Vendor. I have multiple required fields for
saving
a record. How can I enforce that all fields must be entered in order to
save a new record? I have the Required Property set to Yes at the table
level also.
I put the following code in the form BeforeUpdate event to give the user
an
error message if they try to close the form without entering all required
fields(this one is for the Chain Suffix and Region fields) I have done
the
same for each required field. When I try to close the form, I get a bunch
of
error messages, that I click OK on and then the form closes. I know I am
missing something, any suggestions?
If IsNull(Me.Chain_Suffix) = True Then
MsgBox "Please select a Chain Suffix"
Cancel = True
End If
If IsNull(Me.Region_Code) = True Then
MsgBox "Please select a Region"
Cancel = True
End If
.
- Follow-Ups:
- Re: required fields
- From: Lori
- Re: required fields
- From: Lori
- Re: required fields
- Prev by Date: Re: Insert text into field
- Next by Date: Re: Autonumber not working after upsizing
- Previous by thread: Insert text into field
- Next by thread: Re: required fields
- Index(es):