It's not quite as simple as that. When the first Field (either sex or status) is populated the code will fire and you'll get an 'Invalid use of Null' error message, because the other Field is Null, at this point. So you need to only run the code if both Fields have data in them.
You also need to use the Form_Current event to be sure that the Visibility of MaidenName is appropriate for that Record, else it will be formatted according to the condition of the previous Record viewed.
The same thing applies to a New Record. If you don't address this, the Visibility of the Field will depend on the Visibility of the last Record viewed. In the code below, in the Form_Current event, I set the Visibility, on a New Record, to True. That's a personal preference, so you can change that to False, if you like.
Code:
Private Sub Sex_AfterUpdate()
If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
End If
End Sub
Private Sub Status_AfterUpdate()
If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
End If
End Sub
Private Sub Form_Current()
If Me.NewRecord Then
Me.MaidenName.Visible = True
End If
If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
End If
End Sub
Also note that you can only do this, in Code, if this is a Single View Form. For a Datasheet View Form or a Continuous View Form, you'll have to use the Enabled Property and Conditional Formatting, but we'll address that only if we need to.
Linq ;0)>
The problem with making anything foolproof...is that fools are so darn ingenious!
All posts/responses based on Access 2003/2007