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
 
Case "Frame" does nothing, but it does continue the loop and the next pass will set ReallyActiveControl to the Frame's ActiveControl
Case "MultiPage" sets ReallyActiveControl to the current Page of the MultiPage, continues the loop and the next pass will set ReallyActiveControl to that Page's ActiveControl
Case Else exits the loop and returns the final value for ReallyActiveControl.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Case "Frame" does nothing, but it does continue the loop and the next pass will set ReallyActiveControl to the Frame's ActiveControl
Case "MultiPage" sets ReallyActiveControl to the current Page of the MultiPage, continues the loop and the next pass will set ReallyActiveControl to that Page's ActiveControl
Case Else exits the loop and returns the final value for ReallyActiveControl.
Forgive me Mike, I guess I'm struggling with the loop. If Set ReallyActiveControl = Userform1.ActiveControl returns frame each time, wouldn't the loop be infinite?
 
Upvote 0
Forgive me Mike, I guess I'm struggling with the loop. If Set ReallyActiveControl = Userform1.ActiveControl returns frame each time, wouldn't the loop be infinite?
Oops, my very bad. That line should be outside the loop to initialize things

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
 
Upvote 0
Okay, and somewhere in the loop would you not have to do something like "Set ReallyActiveControl = ReallyActiveControl.ActiveControl"? I don't see where the object is getting changed within the loop (other than the MutliPage case of course).
 
Upvote 0
My Typeing code from memory and not testing gives poor results.
VBA Code:
Function ReallyActiveControl() as MS.Forms.Control
    Dim Halt as Boolean

    Set ReallyActiveControl = Userform1

    Do Until Halt
        Set ReallyActiveControl = ReallyActiveControl.ActiveControl
        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
 
Upvote 0
lol... no worries. It was, in fact, a good exercise for me to parse and try to understand it - I appreciate you hanging in there.
 
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