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 gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making one field required based on the value of another field

Status
Not open for further replies.

emzadi

IS-IT--Management
Feb 13, 2001
69
US
How do I make one field required if another field is filled in, but not required if that other field is left blank?

Thanks! Susan M. Wagner
LAPELS
susanw@lapels.com
 
Add code in the On Current event procedure for the form to test the value of the control in question. If it's not null then display a message, cancel the event, and take the user to the control that must be filled in.

Private Sub Form_Current()
If Not IsNull(Me.Control1) Then
If IsNull(Me.Control2) Then
MsgBox "You must enter a value for Lot #", vbInformation
DoCmd.CancelEvent
DoCmd.GoToControl "Control2"
Exit Sub
End If
End If
End Sub
 
I would place any "validation Code" in an event triggered by the EXIT from the form (e.g. BEFOREUPDATE). This allows the user to do what they want without annoying 'Stuffffff'. If they are familiar with the requirements, they will fill in the correct (e.g. necessary) fields. If they FAIL to do it properly, you can alert them to any/all probelms (and usually suggest crrections?) at one time.


MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top