Make Enable Property = OptionButton.Value for all Controls in Fram

R

RyanH

I have an optionbutton in a frame along with a variety of controls. I would
like to enable = True when the Option Button = True and visa versa. The code
below works, but only for TextBoxes. Can this code work for all controls?

Private Sub OptionButton13_Click()

Dim ctrl As Control

For Each ctrl In Me.Frame2.controls
If TypeOf ctrl Is MsForms.TextBox Then
ctrl.Enabled = OptionButton13.Value
End If
Next

End Sub

Thanks,
Ryan
 
S

Susan

you're close.......... try this, i think the logic is right. check
the option button value FIRST, then loop through your controls.
===========================
Private Sub OptionButton13_Click()

Dim ctrl As Control

if optionbutton13.value = true then
For Each ctrl In Me.Frame2.controls
If TypeOf ctrl Is MsForms.TextBox Then
ctrl.Enabled = true
End If
Next
end if

End Sub
======================
(someone may have a better idea)
susan
 
R

RyanH

I need this loop to work for ALL controls. I have 2 other option buttons, 3
checkboxes, 4 comboboxes, 12 labels all in a frame. I want the enable value
for all these controls to equal optionbutton13.Value. Is this possible?

Thanks,
Ryan
 
S

Susan

i think you'll have to specify each type of control.

If TypeOf ctrl Is MsForms.combobox Then
ctrl.enabled = true
end if

If TypeOf ctrl Is MsForms.label Then
etc
If TypeOf ctrl Is MsForms.optionbutton Then
etc
If TypeOf ctrl Is MsForms.checkbox Then
etc

AFAIK you can't select all forms of controls without identifying them
separately.
but i could be wrong.
susan
 
R

RyanH

I was trying to avoid that, but I guess it will work. I currently have all
the controls as a collection, but I was just hoping for a more simple was of
coding it.

Thanks for the help,
Ryan
 
S

Susan

i've got 2 other ideas that might help.........
#1 - why not make the entire frame visible or not visible based on
that optionbutton13? that would take care of all those pesky
individual controls. unless, of course, optionbutton13 is INSIDE the
frame already.......
#2 - why not give all those controls a group name - then you won't
have to loop thru each control as a type, just as a group name.

sorry i wasn't of more assistance. try also searching the newsgroup
for control collection & maybe something else will come up.
susan
 
T

Tim Zych

You could also assign each control a tag value and then loop through all
controls.

Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.Tag = "EnableDisableMe" Then
...
End If
Next
 
R

Rick Rothstein \(MVP - VB\)

Did you see my reply to your other post? It explains the problem and offers
a solution.

Rick
 

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