Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required fields based on another field 1

Status
Not open for further replies.

soccer9

Technical User
Apr 29, 2010
2
US
I have two fields that I am working with, I need to make one field required if the other has data. First, there is a field named "Cancelled" which is a check box. Second, there is a field named "Cancelled Type" which is a combo box field with values to choose. My task is simple, if the "Cancelled" check box has a check in it, then I need the form to REQUIRE the user to put in data in the "Cancelled Type" field before going to other fields.

I didn't see a way to do this in the properties section of the form or the fields within the form. I am not an Access developer and I don't know any 'code'. If the best way to complete this task is to write code, please let me know exactly what code to write AND exactly where to place it.

I will like to be able to copy and paste this code into the database, and use it in different areas to make other fields required based on other fields containing a check mark.

Thanks!
 
Put your form in design view.
Go to the form properties (view, properties)
On the event tab find Before Update. Select Event Procedure from the dropdown that appears when you click in that property. Then click the ellipses to the right.
Copy and paste this code as the spot where your cursor is (it will be between the line that reads Private Sub Form_BeforeUpdate(Cancel as integer) and End Sub)


If Me.[Cancelled] = True Then
If IsNull(Me.[Cancelled Type]) Or Len(Me.[Cancelled Type]) = 0 Then
MsgBox "You must fill in the cancelled type", vbOKOnly, "Data entry is not complete"
Cancel = True

End If

End If

Save your form.
 
The code that you provided worked for me... (THANK YOU!) but since I had to put the code in at the Form properties level and not within the Field properties level, I have another question. I have other relationships on the SAME form that have a check box field and a 'Type' field that is required if the corresponding check box is filled in. How do I expand the code to include those other fields?

For example:

If "Cancelled" (check box) is filled in, then "Cancelled Type" becomes required. ---- ALREADY DONE!

If "Documents Loaded" (check box) is filled in, then "Documents Type" becomes required.

If "Documents Sent" (check box) is filled in, then "Documents Sent Date" becomes required.


Thanks again for all your help!
 
If Me.[Cancelled] = True OR Me.[Documents Loaded] = True OR me.[Documents Sent] = True Then
If IsNull(Me.[Cancelled Type]) Or isnull(me.[Documented Loaded]) or isnull(me.[Documents Sent]) Then
MsgBox "You haven't fill out the form completely", vbOKOnly, "Unable to save"
Cancel = True

End If

End If

(If that doesn't work, replace the Isnull(field) part with Len(field) = 0. Depending on how your database is set up, the field is either NULL when it's not complete, or it might be an empty string - hence the check for the length equal to zero. I'm guessing that's overkill and just the isnull check is good enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top