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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Class Module to capture multiple combobox clicks

Status
Not open for further replies.

chrishooper

Technical User
Oct 11, 2002
20
GB
I am trying to adapt some code that i ripped from the web to capture commands from multiple comboboxes in a userform, the ultimate goal being to allow them to utilise the same subroutine.

I have two comboboxes and have set the tags to "1" and "2" and am trying to get the class module to return the tag names in a dialogue box. Here's my code:

MODULE *******

Option Explicit
Dim combos() As New Class1

Sub ShowDialog()
Dim combocount As Integer
Dim ctl As Control

'Create the combo objects
combocount = 0
For Each ctl In UserForm1.Controls

combocount = combocount + 1
ReDim Preserve combos(1 To combocount)
Set combos(combocount).ComboGroup = ctl

Next ctl
UserForm1.Show

End Sub

CLASS MODULE *******

Public WithEvents ComboGroup As ComboBox

Private Sub ComboGroup_click()
MsgBox "Hello from " & ComboGroup.Tag
End Sub


Since i am new to VBA i only have a vague idea of what the code is doing. I wonder if anyone can help me to understand it and find the mistakes.

Thanks in advance, Chris.
 
I should probably add the original code that I was working on, right? This module and class module does exactly what I want, but with CommandButtons instead of ComboBoxes. I have changed the use of the control name in the code to the Tag property of a ComboBox (since comboboxes do not have captions):

MODULE


Option Explicit
Dim buttons() As New Class1

Sub ShowDialog()
Dim buttoncount As Integer
Dim ctl As Control

'Create the button objects
buttoncount = 0
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "CommandButton" Then
If ctl.Name <> &quot;OKButton&quot; Then 'skip the ok button
buttoncount = buttoncount + 1
ReDim Preserve buttons(1 To buttoncount)
Set buttons(buttoncount).ButtonGroup = ctl
End If
End If
Next ctl
UserForm1.Show

End Sub



CLASS MODULE (class 1)

Public WithEvents ButtonGroup As CommandButton

Private Sub ButtonGroup_Click()
MsgBox &quot;Hello from &quot; & ButtonGroup.Name
End Sub


Chris :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top