Looping through Option buttons

A

Ayo

I am having a problem with this set of code below. What I am trying to do is
to disable all the option buttons in the frame, except for the active option
button, when the form initializes. Or, if no option button is selected in the
frame, I want all the option buttons to be enabled but if one option button
is selected, I want the rest of the option buttons to be disabled.
How can I write the code to do that?

With Me.fraProject
For Each optProject In Me.fraProject
If .optProject.Value = True Then
.optProject.Enabled = True
ElseIf .optProject.Value = False Then
.optProject.Enabled = False
End If
Next
End With
 
S

Susan

what error are you getting? or does it simply not work?

you're close: :)
For Each optProject In Me.fraProject
If optProject.Value = True Then
optProject.Enabled = True
Else optProject.Enabled = False
End If
Next optProject


you don't have to tell it "if it's false" because there's only 2
options, true or false. so if it's not true, then it's obviously
false.

and you don't need the With Me.fraProject line because you're already
telling it with what in the for-each statement.

hope it helps!
susan
 
A

Ayo

I am getting the "Subscript out of range" error. And I can't figure out what
I am doing wrong.
 
S

Susan

you might have been getting that because you were referring to
me.fraproject twice.
do you still get the same error now?
this is in the initialization sub, correct?

IS one of the buttons checked? usually in the initialization sub, you
set everything to false, so you let the user choose which button to
select. since you are doing this in the initilization sub, how are
you setting one of them to true?
susan
 
S

Susan

also, i don't see anything "dimmed".

you'd need
Dim optProject as Control

and i missed this

For Each optProject In fraProject.Controls

you don't need to refer to "me" because you're already in the userform
initialization sub.

:)
susan
 
S

Susan

ok, i think this is what you want.

Private Sub optionbutton1_click()
Call Disable_Controls
End Sub

Private Sub optionbutton2_click()
Call Disable_Controls
End Sub

Private Sub optionbutton3_click()
Call Disable_Controls
End Sub

'add as many as you need for all your control buttons

Sub Disable_Controls
Dim optProject As Control
For Each optProject In fraProject.Controls
If optProject.Value = True Then
optProject.Enabled = True
Else
optProject.Enabled = False
End If
Next optProject
End Sub


no matter which option button is selected, the other ones will become
disabled. this does NOT go in the initialization sub. they are
separate subs within the userform coding.
hope this (finally!) helps
:)
susan
 

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