How do I make a field required in specific cases

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a form to which I want to add the followic logic:
If field1="xxxx" then field2 is required else field2 is not required.

I'm not sure if this is an afterupdate event or validation rule or something
else.
I've tried different things but I can't get to it.

Your help is always appreciatied.
Thanks,
Kanga
 
In the BeforeUpdate event of your form add the following (untested air
code):

Sub Form_BeforeUpdate(Cancel As Integer)
If Me.txtField1 = "xxxx" Then
If Len(Me.txtField2 & vbNullString) = 0 Then
MsgBox "Fill in Field2 please", vbOKOnly, "Data Required"
Me.txtField2.SetFocus
Cancel Event
Exit Sub
End If
End If
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
Hi, Kanga.

Use the form's BeforeUpdate event and, optionally, a message in field1's
AfterUpdate event to alert the user that field2 is required:

' field1 AfterUpdate event
If field1 = "xxxx" Then
MsgBox "Field2 is now required."
End If

' Form BeforeUpdate
If (field1 = "xxxx" AND Nz(field2) = 0) Then
Cancel = True
MsgBox "Please enter a value for field2"
Me!Field2.SetFocus
End If

Hope that helps.
Sprinks
 
Back
Top