If Statement

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

Guest

I have a complex "If" statement that I need to write but I don't really know
how. I've tried many different approaches but none seem to work. Here's what
I need to do. I have 3 controls on my form that I need to toggle between
depending on if 2 other controls have entries in them or not.

First if Field A has an entry and Field B doesn't then I need control 1 to
be visible and control 2 & 3 not to be visible. If Field A doesn't have an
entry and Field B does then I need control 2 to be visible and control 1 & 3
not to be visible. Then lastly if Field A & B don't have any entries then I
need control 3 only to be visible.

How would I write this and where do I need to place this code on my form?
 
Secret said:
I have a complex "If" statement that I need to write but I don't
really know how. I've tried many different approaches but none seem
to work. Here's what I need to do. I have 3 controls on my form that
I need to toggle between depending on if 2 other controls have
entries in them or not.

First if Field A has an entry and Field B doesn't then I need control
1 to be visible and control 2 & 3 not to be visible. If Field A
doesn't have an entry and Field B does then I need control 2 to be
visible and control 1 & 3 not to be visible. Then lastly if Field A &
B don't have any entries then I need control 3 only to be visible.

How would I write this and where do I need to place this code on my
form?

There would be a number of methods that accomplish this. What do you want
to happen when BOTH FieldA and FieldB have entries? That could make a
difference in what methods would be used.

If both fields having entries will never happen then the visibility of
Control1 simply mirrors the return of IsNull() on FieldA and the visibility
of Control2 likewise mirrors the IsNull() return for FieldB.

Me!Control1.Visible = Not IsNull(FieldA)
Me!Control2.Visible = Not IsNull(FieldB)
Me!Control3.Visible = (IsNull(FieldA) AND IsNull(FieldB))

You would need this in the AfterUpdate event of both FieldA and FieldB and
in the form's Current event. I would put it in a function or sub and call
it from those three events.
 
Secret,
I think this should do it. I tested this, and it should be OK. If any
problems, you can at least see the logic, and correct if necessary.
Private Sub Form_Current()
CheckFields
End Sub

Private Sub CheckFields()
Field1.Visible = Not IsNull(FieldA)
Field2.Visible = IsNull(FieldA) And Not IsNull(FieldB)
Field3.Visible = IsNull(FieldA) And IsNull(FieldB)
End Sub

Private Sub FieldA_AfterUpdate()
CheckFields
End Sub

Private Sub FieldB_AfterUpdate()
CheckFields
End Sub

BUT... you don't have any logic for BOTH A and B. If that situation may
occur, you'll need to code for that condition, or code to prevent having an
A and B at the same time.
 
FieldA & FieldB will never have entries at the same time. They are driven
from another field that will "toggle" them on or off depending on whether
there is an entry in that other field. I will try out what you said. Thanks
for your help!
 
It worked perfectly! FieldA & FieldB will never have a value at the same time
because they are driven from another field that will toggle them on and off
depending on if there is a value in that other field. Thanks for your help!
 

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

Back
Top