OptionButtons with same GroupName not working properly

R

RyanH

I have 3 Option Buttons on a Userform. Each Option Button is located in a
different frame. Which ever option button the user selects I have code to
enable all the controls in that frame and disable the controls in the other
frames. I gave the 3 Option Buttons the same GroupName but it does not work.
Any one have any ideas?

Thanks,
Ryan
 
R

Rick Rothstein \(MVP - VB\)

You can't put each OptionButton IN a different Frame and get them to work as
a group, no matter what you assign to the GroupName property. All
OptionButtons within the **same** Frame operate as a group **independent**
from any other OptionButtons placed in different Frames (the word "Frame" is
meant to include the UserForm itself as well). If you want to use
OptionButtons to control things as you have described, then you will have to
move them outside of the Frames so they sit on the UserForm itself. You can
place them *near* their respective Frames, just not *on* them. If you choose
to do that, here is how to make them all work together properly.

First off, move the OptionButtons off the Frames and position them where you
want. Next, we will need a way to identify which controls are on which
Frame. We will do that using the Tag property to hold a common
identification string that we can check within our code. To do this, click
any control in Frame1 and then press Ctrl+A to select all the controls
within that Frame and then set the Tag property for the selected controls to
the name of the OptionButton you want to control them (for example, type
OptionButton1 into the Tag property for the selected controls if you are
using default names). Also, select the Frame itself and set its Tag property
to the name for the OptionButton too (it seems that Ctrl+A selects the
controls inside the Frame, but not the Frame itself). Now, repeat this
procedure for your other Frames (using the OptionButton name for the
controlling OptionButton in the Tag property for their respective controls).
Next, copy/paste this code into the UserForm's code window...

'************** START OF CODE **************
Private Sub UserForm_Initialize()
OptionButton1.Value = True
'
' Place rest of your UserForm_Initialize event code here
'
End Sub

Private Sub OptionButton1_Click()
EnableAndDisableControls OptionButton1
End Sub

Private Sub OptionButton2_Click()
EnableAndDisableControls OptionButton2
End Sub

Private Sub OptionButton3_Click()
EnableAndDisableControls OptionButton3
End Sub

Private Sub EnableAndDisableControls(OptButton As MSForms.OptionButton)
Dim C As Control
For Each C In Me.Controls
If Not TypeOf C Is MSForms.OptionButton Then
If C.Tag = OptButton.Name Then
C.Enabled = True
Else
C.Enabled = Not OptButton.Value
End If
End If
Next
End Sub
'************** END OF CODE **************

That is it. Run your UserForm and it should do what you want.

Rick
 
R

RyanH

Thanks for explaining how all that works. I do have one question though.
What is the difference in defining a variable like this:

Private Sub EnableAndDisableControls(OptButton As MSForms.OptionButton)

End Sub

and this,

Private Sub EnableAndDisableControls()

Dim OptButton As MSForms.OptionButton

End Sub


I appreicate your input! Thanks,
Ryan
 
R

Rick Rothstein \(MVP - VB\)

Let's look at the second case first. The Dim statement creates the variable
for use solely within the subroutine... nothing outside of the subroutine
can see it nor change its value... that can only be done by code executing
within the subroutine.

However, in the first case, the variable is actually an argument to be
passed into the subroutine when you call it... everything I said about the
second case above applies **except** that you get to set the "variable's"
initial value at the time you call the subroutine, before any code inside
the subroutine even begins to execute.

Here is a little more detail about the process...

http://en.wikiversity.org/wiki/Functions_and_Subroutines_in_VB6

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