Field dependent on 2 other fields

K

KateB

Hi,

I have 2 fields, Nature_of_advice1 and Nature_of_advice2. Either of these
fields can select the option "other" which will then show fields Other1 or
Other2 for completion. This part works fine! However, if the user selects
"other infection state" in either of the first 2 fields mentioned then they
need to complete a third field which I would like to be hidden unless needed.

I have an after update event for both fields to show/hide the secondary
fields, and have put the same info into the form's OnCurrent field. It all
seems to work OK unless I select "other infection state" in
Nature_of_advice1. If I leave the form and come back to it, the "other
infection state" field is hidden again.

Can anyone suggest where I might be going wrong? I'm not sure if its in the
form event or field event. I suspect its something to do with order? I've
posted the VBA script below, but I should point out I'm a novice so please be
simple with me!!

Kate

Private Sub Form_Current()
If Me.Nature_of_advice1 = "Other" Then
Me.Other1.Visible = True
Else
Me.Other1.Visible = False
End If

If Me.Nature_of_advice2 = "Other" Then
Me.Other2.Visible = True
Else
Me.Other2.Visible = False
End If

If Me.Nature_of_advice1 = "Other infection state" Then
Me.Other_infec_state.Visible = True
Else
Me.Other_infec_state.Visible = False
End If
If Me.Nature_of_advice2 = "Other infection state" Then
Me.Other_infec_state.Visible = True
Else
Me.Other_infec_state.Visible = False
End If

End Sub
 
K

Klatuu

Your code for Nature_of_advice2 is making Other_infec_state invisible even
after you set it in Nature_of_advice2. Change it to this:

Private Sub Form_Current()

If Me.Nature_of_advice1 = "Other" Or Me.Nature_of_advice2 = "Other" Then
Me.Other1.Visible = True
Else
Me.Other1.Visible = False
End If

If Me.Nature_of_advice1 = "Other infection state" OR
Me.Nature_of_advice2 = "Other infection state" Then
Me.Other_infec_state.Visible = True
Else
Me.Other_infec_state.Visible = False
End If

End Sub

Also, notice the indentation. It make the code much easier to read and
easier to find errors like this.
 
K

KateB

Many thanks - works perfectly!

I tried using "OR" before I posted but it kept giving me errors. It seems
that they were just because I was doing a line break where it didn't like it
as I got the same error when I tried to copy your layout. If only Access's
error tips were a bit clearer!! Have taken on board your comments re:
indentation too.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top