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
Userform Code
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