Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OptionButton Click Event using a Class Module

Status
Not open for further replies.

bdmangum

Technical User
Dec 6, 2006
171
US
Howdy,

In the past I've used a Class Module in order to create change events for comboboxes which were programmatically created. I would like to use this same process to create a click event for OptionButtons which are programmatically created.

Here's what I've done so far. The error occurrs at the bolded line. It returns a Run-time error '13', Type Mismatch.
Code:
Private ppOps As New Collection
Private ctlOps As ppCB
Private WithEvents ppOpBut1 As MSForms.OptionButton
Private WithEvents ppOpBut2 As MSForms.OptionButton


Set ctlOps = New ppCB
[b]ctlOps.Init ppOpBut1, Me[/b]
ppOps.Add ctlOps
    
Set ctlOps = New ppCB
ctlOps.Init ppOpBut2, Me
ppOps.Add ctlOps
    
ppOps.Add ppOpBut1
ppOps.Add ppOpBut2

Here's the class module:
Code:
Option Explicit

Private WithEvents m_OB As MSForms.OptionButton
Private m_Form As Summary_Final

Public Sub Init(ctl As OptionButton, frm As Summary_Final)
Set m_OB = ctl
Set m_Form = frm
End Sub

Private Sub m_OB_Click()
m_Form.Opt m_OB
End Sub

Private Sub Class_Terminate()
Set m_OB = Nothing
Set m_Form = Nothing
End Sub

What part is incorrect in the code? Obiviously I'm not seeing why it won't work.
 
why using ppCB instead of m_OB ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH,

Not sure I understand your question. ppCB is the name of the class module. What exactly are you asking?
 
OOps, sorry for the misunderstanding :~/
So, ppOpBut1 is a MSForms.OptionButton and Me an UserForm ?
You may try this:
Public Sub Init(ctl As [!]MSForms.[/!]OptionButton, frm As Summary_Final)

BTW, what is a Summary_Final ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PH, that did the trick. Adding MSForms worked like a charm. Summary_Final is the Name of the userform. I coded it as such in order that only that form could use the function.

BTW does anyone know how to do cascading "For Each Item In Collection" loops? Meaning I need to use that command within itself, is there another control variable other than "Item" which I can use?

For example I have this, which returns an error.
Code:
Dim TempText As String
Dim TempLen As Long

[b]For Each Item In ppOps[/b]
    If Left(Item.Name, 3) = "One" Then
        TempLen = Len(Item.Name) - 3
        TempText = Right(Item.Name, TempLen)
        
        If Item.Value = True Then
            For Each Item In ppOps
                If Item.Name = TempText Then
                    Item.Visible = True
                    Item.Locked = False
                End If
            Next
        Else
        [b]For Each Item In ppOps[/b] [i]Error at this line[/i]
                If Item.Name = TempText Then
                    Item.Visible = False
                    Item.Locked = True
                End If
            Next
        End If
    End If
Next
 
You may use any variable name you want !

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Please note that the collection contains instances of ppOB objects rather than option buttons. You can expose the control by public declaration (Public WithEvents m_OB As MSForms.OptionButton) or creating a r/o property, and next (public declaration case, use it as property):
..
If Item.m_OB.Name = TempText Then
..
The alternative is to write your own 'Name' property procedure (and other required too) in ppOB class module.

I would avoid to use 'Item' as temporary object reference.
If you pass event handling to the class module, there is co need to declare ppOpBut1 and ppOpBut2 using WithEvents (just 'Private ppOpBut1 As MSForms.OptionButton' is enough).

combo
 
Correct combo. When I posted the last code I had forgotten to do the Name check. I went with: If TypeName(Item) = "OptionButton" Then... And like wise for the Textbox part.

Thanks for all the help y'all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top