Two if statements in Form Current code

G

GD

I'm new to code, but trying hard to understand the lingo. What's wrong with
my code below? I'm trying to get two sections of a form to be visible or
invisible, depending on whether their respective check boxes are checked. I
had it working fine with just one.

Private Sub Form_Current()
If Check195 = True Then
Combo206.Visible = True
Combo269.Visible = True
Text267.Visible = True
Label300.Visible = True
Else
Combo206.Visible = False
Combo269.Visible = False
Text267.Visible = False
Label300.Visible = False

If Check209 = True Then
Combo213.Visible = True
Text215.Visible = True
Text217.Visible = True
Label221.Visible = True
Combo219.Visible = True
Else
Combo213.Visible = False
Text215.Visible = False
Text217.Visible = False
Label221.Visible = False
Combo219.Visible = False
End If

End Sub

Thanks for your valuable time!!
 
M

MikeJohnB

You are missing an End If after the first if

Private Sub Form_Current()
If Check195 = True Then
Combo206.Visible = True
Combo269.Visible = True
Text267.Visible = True
Label300.Visible = True
Else
Combo206.Visible = False
Combo269.Visible = False
Text267.Visible = False
Label300.Visible = False
End If 'Added End If Here

If Check209 = True Then
Combo213.Visible = True
Text215.Visible = True
Text217.Visible = True
Label221.Visible = True
Combo219.Visible = True
Else
Combo213.Visible = False
Text215.Visible = False
Text217.Visible = False
Label221.Visible = False
Combo219.Visible = False
End If

End Sub

I guess there would be better ways of writing the code using a "With" Clause
but I don't see why your code wont work as it is with the added end if (You
always have to close an If With An End If but the code should have thrown an
error saying no end if?)

Are you getting any other errors showing when you open the form? Is the On
current the correct event to use?

Regards

Mike B
 
J

John... Visio MVP

What about something like:

Private Sub Form_Current()

Combo206.Visible = Check195
Combo269.Visible = Check195
Text267.Visible = Check195
Label300.Visible = Check195

Combo213.Visible = Check209
Text215.Visible = Check209
Text217.Visible = Check209
Label221.Visible = Check209
Combo219.Visible = Check209

' This assumes Check195 and Check209 are boolean

John... Visio MVP
 
D

Dale Fye

Because the value of the checkboxes would evaluate to True or False, I would
write this as the following. I would put this code in the forms current
event, and then would copy the two parts and put the code that has to do with
Check195 in the AfterUpdate event of that checkbox, and the code that has to
do with Check209 in the AfterUpdate event in that checkbox.

Combo206.Visible = Check195
Combo269.Visible = Check195
Text267.Visible = Check195
Label300.Visible = Check195Combo213.Visible = Check209
Text215.Visible = Check209
Text217.Visible = Check209
Label221.Visible = Check209
Combo219.Visible = Check209

BTW, I would strongly recommend that you use one of the standard control
naming conventions to name your controls. The way you have it, it is
impossible to tell what the various controls are about. Google on "VBA
+naming +conventions" to see what I'm talking about.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
G

GD

Dale: The DB I'm working with right now is my test case, not one I intend to
save. I intend to name in those.

I like your solution! Can I ask you something else? How would I go about
coding something on my list below to be visible if there was more than one
criteria, i.e. the box is checked AND the $ amount of, say, Text1 is less
than $2000?

I appreciate your time and effort!
 

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