connection between checkbox and combo box

L

LJB

I have a drop down box with a selection for each state. If PA is selected,
there is a corresponding check box that is to appear. I have the code as
follows:

If STATE = "PA" Then Checkbox1.Visible = True

However, if the user then changes the selection to another state, the
checkbox stays visible. I know that I need an additional line to my code
that makes the checkbox not visible (visible = false) if PA is not selected-
I'm having trouble coming up with the code.

Thank you for any assistance.
 
A

Andi Mayer

I have a drop down box with a selection for each state. If PA is selected,
there is a corresponding check box that is to appear. I have the code as
follows:

If STATE = "PA" Then Checkbox1.Visible = True

However, if the user then changes the selection to another state, the
checkbox stays visible. I know that I need an additional line to my code
that makes the checkbox not visible (visible = false) if PA is not selected-
I'm having trouble coming up with the code.

Thank you for any assistance.
If STATE = "PA" Then
Checkbox1.Visible = True
else
Checkbox1.Visible = False
endif
 
L

Lynn Trapp

If STATE = "PA" Then
Checkbox1.Visible = True
ELSE
Checkbox1.Visible = False
End If
 
C

Chris Reveille

Try
If me.STATE = "PA" Then Me.Checkbox1.Visible = True
else
Me.Checkbox1.Visible = False


Chris
 
B

BruceM

You should get what you need with code something like the following in the
After Update event for the combo box (which I will call cboState):

If Me.cboState = "PA" Then
Me.Checkbox1.Visible = True
Else: Me.Checkbox1.Visible = False
End If

You will need the same code in the form's On Current event.

I think the code will also work if you use the name of the field (e.g.
Me.State) instead of the name of the combo box. I'm not clear on whether one
has an advantage over the other.
 
L

Lynn Trapp

If Me.cboState = "PA" Then
Me.Checkbox1.Visible = True
Else: Me.Checkbox1.Visible = False
End If

Bruce,
You will likely get an error with this code because of the colon following
the word "Else".
I think the code will also work if you use the name of the field (e.g.
Me.State) instead of the name of the combo box. I'm not clear on whether one
has an advantage over the other.

"Me" is a reference to the currently active form object. Therefore, if
[State] is not an object on the currently active form, then it will generate
an error. When objects are placed on a form using the field list they are
given the same name as the field in the record source and, thus, it may well
be that he could use Me.State.
 
B

BruceM

Lynn, thanks for taking the time to comment. The colon puts itself in there
when I type in the VBA window. If I delete it, it comes back. So far it has
worked as written in all of the various situations in which I have used it.

Lynn Trapp said:
If Me.cboState = "PA" Then
Me.Checkbox1.Visible = True
Else: Me.Checkbox1.Visible = False
End If

Bruce,
You will likely get an error with this code because of the colon following
the word "Else".
I think the code will also work if you use the name of the field (e.g.
Me.State) instead of the name of the combo box. I'm not clear on whether one
has an advantage over the other.

"Me" is a reference to the currently active form object. Therefore, if
[State] is not an object on the currently active form, then it will generate
an error. When objects are placed on a form using the field list they are
given the same name as the field in the record source and, thus, it may well
be that he could use Me.State.

--
Lynn Trapp
MS Access MVP
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm
 
A

Andi Mayer

Lynn, thanks for taking the time to comment. The colon puts itself in there
when I type in the VBA window. If I delete it, it comes back. So far it has
worked as written in all of the various situations in which I have used it.
this is normal, because the else should be on a single line, the :
tells you:
it's a new command on the same line like

rs.movefirst:debug.print rs.recordcount:rs.movelast

but if you write on a single line
If Me.cboState = "PA" Then Me.Checkbox1.Visible = True Else
Me.Checkbox1.Visible = False

it doesnt give you the :
 
L

Lynn Trapp

Interesting. I've never seen that happen before but, then, my experience
isn't universal. The colon marks that as a Label and, apparently, is not
interfering with the execution of the code.

--
Lynn Trapp
MS Access MVP
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm


BruceM said:
Lynn, thanks for taking the time to comment. The colon puts itself in there
when I type in the VBA window. If I delete it, it comes back. So far it has
worked as written in all of the various situations in which I have used it.

Lynn Trapp said:
If Me.cboState = "PA" Then
Me.Checkbox1.Visible = True
Else: Me.Checkbox1.Visible = False
End If

Bruce,
You will likely get an error with this code because of the colon following
the word "Else".
I think the code will also work if you use the name of the field (e.g.
Me.State) instead of the name of the combo box. I'm not clear on
whether
one
has an advantage over the other.

"Me" is a reference to the currently active form object. Therefore, if
[State] is not an object on the currently active form, then it will generate
an error. When objects are placed on a form using the field list they are
given the same name as the field in the record source and, thus, it may well
be that he could use Me.State.

--
Lynn Trapp
MS Access MVP
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm
 
A

Andi Mayer

Interesting. I've never seen that happen before but, then, my experience
isn't universal. The colon marks that as a Label and, apparently, is not
interfering with the execution of the code.

this has nothing to do with a label, it's only a lineSaver

for a label it has to be last character on this line
 
B

BruceM

Also, I think a label needs to start all the way to the left margin of the
code window.
 
L

Lynn Trapp

this has nothing to do with a label, it's only a lineSaver
for a label it has to be last character on this line


I have no doubt you are right there. I have just never seen VBA
automatically add a colon after the word Else in an If statement.
 
L

Lynn Trapp

Bruce,
I've used every version of Access, since version 2 and haven't seen that in
any of them. Like I said, though, my experience isn't necessarily universal,
so perhaps someone else has seen that.
 
L

Lynn Trapp

Bruce,
It turns out it is all my coding style that has prevented me from seeing
that. I NEVER put the statement to be executed on the same line as the word
"Else."
 
Top