A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

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



 
 
Thread Tools Display Modes
  #1  
Old April 24th, 2007, 05:19 AM
Jeff Monroe Jeff Monroe is offline
Member
 
First recorded activity by OfficeFrustration: May 2006
Location: San Diego, CA
Posts: 27
Default Requiring Data in a field based on another field's entry

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
  #2  
Old April 24th, 2007, 06:24 AM posted to microsoft.public.access.forms
Allen Browne
external usenet poster
 
Posts: 11,706
Default Requiring Data in a field based on another field's entry

"Jeff Monroe" wrote in message
...

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.

--
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.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 03:03 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.