Requiring Data in a field based on another field's entry

J

Jeff Monroe

I have two fields on a form: NCR_No and Insp_Date. I want to make it so
if a user enters an NCR_No, an error message appears if the user did
not enter an Insp_Date telling them an Insp_Date is required.

How can I accomplish this?

Jeff
 
A

Allen Browne

Jeff Monroe said:
I have two fields on a form: NCR_No and Insp_Date. I want to make it so
if a user enters an NCR_No, an error message appears if the user did
not enter an Insp_Date telling them an Insp_Date is required.

Solution A: Validation Rule on the Table
=============================
Use this if the user *must* always enter a date if NCR_No is entered.

1. Open your table in design view.

2. Open the Properties box (View menu.)

3. Beside the Validation Rule in the Properties box, enter:
([NCR_No] Is Null) OR ([Insp_Date] Is Not Null)
Be sure to use the rule in the Properties box, not the one in the lower pane
of table design (which applies to one field.)

4. Enter the message you want in the Validation Text property, e.g.:
You must enter the Inspection Date if you enter an NCR Number.

More about validation rules:
http://allenbrowne.com/ValidationRule.html

Solution B: BeforeUpdate event procedure of the form
======================================
Use this to give a warning, but let the user override it.

1. Open the Form in design view.

2. Set the Before Update property of the *form* (not of a text box) to:
[Event Procedure]

3. Click the Build button (...) beside the property.
Acess opens the code window.

4. Set up the event procedure like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If IsNull(Me.[Insp_Date]) And Not IsNull(Me.[NCR_No]) Then
strMsg = "NCR number, but no inspection date." & vbCrLf & _
"Continue anyway?"
If MsgBox(strMsg, vbYesNo + vbDefaultButton2, _
"Are you sure?") = vbNo Then
Cancel = True
'Me.Undo
End If
End If
End Sub

Solution C: Automatically enter the date
=============================
Use the After Update event procedure of NCR_No to assign today's date to the
Insp_Date:
If IsNull(Me.[Insp_Date]) And Not IsNull(Me.[NCR_No]) Then
Me.[Insp_Date] = Date
End If

You probably want to use Solution A or B as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top