Multiple Option Button in Frame Click Event

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
So I have 6 Option Buttons in a frame on a userform and I want to simply click any of the six and it will tell me the name of the option button (obviously I want to do something more after I get that done).

To do this, common practice is to set up an array of a class for the option buttons. In this example, I want to be able to run methods from the parent userform from the class but for some reason the mfrmParent object is not being recognized? I thought this would be straight forward as I have it working for other controls in a similar way. Ideas?

Class Code
VBA Code:
Option Explicit
 
Public WithEvents DepartGroup As MSForms.OptionButton
 
Private mfrmParent As Object

'--public properties
Public Property Set Parent(frmParent As Object)
    Set mfrmParent = frmParent
End Property

'--event procedures

Private Sub DepartGroup_Click()
 '--store selection in roadmap property

DepartGroup.mfrmParent.SelectedDepart = DepartGroup.Name  'Getting an error here as mfrmParent doesn't seem to be set properly?
DepartGroup.mfrmParent.DepartSet

End Sub

Userform Code
VBA Code:
Option Explicit

'Private Userform Variables
Private msSelectedDepart As String
Private DepartChoice() As New clsOptionBtn

'Userform Public Properties
Public Property Let SelectedDepart(sSelectedDepart As String)
    msSelectedDepart = sSelectedDepart
End Property

Private Sub Userform_Initialize()

Dim ctrl As control
Dim lCount As Long

For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
        lCount = lCount + 1
        ReDim Preserve DepartChoice(1 To lCount)
        Set DepartChoice(lCount).DepartGroup = ctrl
        'link to this instance of the userform
        Set DepartChoice(lCount).Parent = Me
    End If
Next ctrl

End Sub

Public Sub DepartSet()

MsgBox "You have clicked on " & msSelectedDepart

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Since SelectedDepart is a property of the userform, not the optionbutton, try . . .

VBA Code:
mfrmParent.SelectedDepart = DepartGroup.Name  
mfrmParent.DepartSet

Hope this helps!
 
Upvote 0
Solution
Since SelectedDepart is a property of the userform, not the optionbutton, try . . .

VBA Code:
mfrmParent.SelectedDepart = DepartGroup.Name 
mfrmParent.DepartSet

Hope this helps!
That did it - thanks! Odd though, that earlier convention worked for other examples like when I used a group of command buttons.

This class worked

VBA Code:
Option Explicit
 
Public WithEvents MonthGroup As CommandButton
 
Private mfrmParent As Object

'--public properties
Public Property Set Parent(frmParent As Object)
 Set mfrmParent = frmParent
End Property

'--event procedures

Private Sub MonthGroup_Click()
 '--store selection in roadmap property
 
MonthGroup.Parent.SelectedMonth = MonthGroup.Name  
MonthGroup.Parent.MonthSet  

End Sub
 
Upvote 0
In your original example, mfrmParent is the property of the class object, not the option button. And so DepartGroup.mfrmParent will cause an error saying that the object (DepartGroup) doesn't support this property or method (mfrmParent).

In your subsequent example, the Parent property refers to the parent property of the the command button, not the class object. And so the parent property of the command button is the userform, and so those lines succeed.
 
Upvote 0
There is no need to set the Parent property, since that information can be deduced. A check box's parent is inherent in the checkbox itself. It should be deduced rather than declared.

When working with Frames (and multipages), I've found these two functions to be useful

This drills up from a control, through all of the enclosing containers (Frames and Pages) and returns the userform the control is in.
VBA Code:
Dim someCotrol as msForms.Control

Function UFParent() As Object
    Set UFParent = someControl
    On Error Resume Next
    Do
       Set UFParent = UFParent.Parent
    Loop Until Err
    On Error Goto 0
End Function

This drills down from a Userform, through all the Frames and Multipages, which would be returned by .AcitiveControl properties, and returns the control
VBA Code:
Function ReallyActiveControl() as MS.Forms.Control
    Dim Halt as Boolean

    Set ReallyActiveControl = Userform1.ActiveControl

    Do Until Halt
        
        Select Case TypeName(ReallyActiveControl)
            Case "Frame"
            Case "MultiPage"
                With ReallyActiveControl
                    Set ReallyActiveControl = .Pages(.Value)
                End With
            Case Else
                Halt = True
        End Select
    Loop
End Function

' edited from original post.

In use, how the control/userform were passed to the fuctions would depend on the application.
 
Last edited:
Upvote 0
Hey Mike, thanks for the functions. I can dissect the first fairly easily but the second one is harder to understand. How does the .ActiveControl property move from the userform down to the control (and which control if there are several)?
 
Upvote 0
If a checkbox is in a frame and is the active control, Userform1.ActiveControl returns the Frame, not the checkbox. But Frame1.ActiveControl is the checkbox in question.
Similarly for MultiPage controls. There is a variation to account for .ActiveControl being a property of a Page, not a Multi-Page.

But ReallyActiveControl will drill down through all of that. (Only a UserForm, Frame and Page have an .ActiveControl property)
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top