If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Detecting Click Event of Dynamically created controls

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-16-09, 09:17
rruthven rruthven is offline
Registered User
 
Join Date: Apr 2009
Posts: 3
Detecting Click Event of Dynamically created controls

I have created a Userform using Excel 2007 VBA. I have written code to add a number of labels and comboboxes to the Userform (dependent on a given number of perosnnel at work). The form looks good! But I am struggling to get the prgramme to return the text from the comboboxes! HELP
Reply With Quote
  #2 (permalink)  
Old 04-16-09, 10:46
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi,

How many comboboxes (the maximum) are we talking about here?
Reply With Quote
  #3 (permalink)  
Old 04-17-09, 03:21
rruthven rruthven is offline
Registered User
 
Join Date: Apr 2009
Posts: 3
The form could have up to 40 comboboxes. Further, I would like to be able to use the spreadsheet with both Excel 2003 & 2007!
Reply With Quote
  #4 (permalink)  
Old 04-17-09, 05:37
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
One option would be to programatically add event handler code to the userform's class module as you add the comboboxes.

Another option would be to add all of the comboboxes at design time, set their visible properties to false, and then just make whichever ones you need visible at runtime.

Given the number of comboboxes (40!!!) another option would be to use a class module to mimic a control array - we can set up a communal event handler shared by all of the comboboxes which saves having to use repetitive code. I've attached a simple demonstration of this which dynamically adds 10 comboboxes and gives a message box when the Change event is raised.

All that's required is a blank userform called ufm_Example.
In the ufm_Example class module resides this code:
Code:
Option Explicit

Const sngUFM_WIDTH As Single = 300
Const sngUFM_HEIGHT As Single = 500

Private pComboboxes As Collection

Private Sub UserForm_Initialize()

    'resize the userform
    Me.Width = sngUFM_WIDTH
    Me.Height = sngUFM_HEIGHT
    
    AddComboboxes
End Sub



Private Sub AddComboboxes()
    Const sngCBX_WIDTH As Single = 100
    Const sngCBX_HEIGHT As Single = 20
    Const intCBX_TO_ADD As Integer = 10
    
    Dim intCounter As Integer
    Dim cbx As c_Combobox
    Dim ctl As MSForms.ComboBox
    
    If pComboboxes Is Nothing Then Set pComboboxes = New Collection
    
    'for this example let's add 10 comboboxes to our userform
    For intCounter = 1 To intCBX_TO_ADD
    
        'add the control
        Set ctl = Me.Controls.Add(bstrProgID:="Forms.ComboBox.1", _
                                    Name:="Combobox" & intCounter, _
                                    Visible:=True)
                      
        With ctl
            'position the combobox
            .Width = sngCBX_WIDTH
            .Height = sngCBX_HEIGHT
            .Left = Me.Left + ((Me.Width - ctl.Width) / 2)
            .Top = Me.Top + (Me.Height / (intCBX_TO_ADD + 2) * intCounter)
            
            
            'populate the combobox
            .List = Sheet1.Range(Sheet1.Cells(2, intCounter), Sheet1.Cells(9, intCounter)).Value
            
            
            Set cbx = New c_Combobox
            cbx.SetCombobox ctl
            pComboboxes.Add cbx
        End With
        
    Next intCounter
    
                                    
    
End Sub
Then all we need is a custom class module called c_Combobox:
Code:
Option Explicit

Public WithEvents cbx As MSForms.ComboBox

Sub SetCombobox(ctl As MSForms.ComboBox)
    Set cbx = ctl
End Sub

'a generic change event handler for our comboboxes
Private Sub cbx_Change()
    If cbx.ListIndex > -1 Then
        MsgBox "You clicked on " & cbx.Name & vbLf & _
                "The value is " & cbx.Value
    End If
End Sub

I have attached an example workbook containing this code so you can download and experiment with it.



I hope that helps...
Attached Files
File Type: zip comboboxes.zip (13.4 KB, 117 views)
Reply With Quote
  #5 (permalink)  
Old 04-19-09, 09:39
rruthven rruthven is offline
Registered User
 
Join Date: Apr 2009
Posts: 3
Thanx to Colin L!!!!!!!!!!!!!!

The routine you submitted is perfect! Thanks a million!
Reply With Quote
  #6 (permalink)  
Old 04-20-09, 04:43
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
You're welcome! Good luck with the rest of your project...
Reply With Quote
  #7 (permalink)  
Old 03-05-12, 08:34
brucergreen brucergreen is offline
Registered User
 
Join Date: Mar 2012
Posts: 2
Just what I was looking for

Hi
I have 210 Possible comboboxes accross 10 pages in a multipage (all created as requred at runtime). So I hope this work as I have not had the time as of yet to test it , but I can not see why it wont

I will let you know how I get on

Thanks again
Reply With Quote
  #8 (permalink)  
Old 03-06-12, 07:14
brucergreen brucergreen is offline
Registered User
 
Join Date: Mar 2012
Posts: 2
I Have A Problem ( can not call procedure from Class)

Hi

The code you provided works fine but when I change the msgbox to a procedure call (Calc_Price cbo.text, cboidx) I can not find the procedure although the same procedure call from the predefined cboboxes works fine

Any suggestions??

Bruce
Reply With Quote
  #9 (permalink)  
Old 03-06-12, 12:46
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
hi Bruce,

It's probably because of the scope of the Calc_Price procedure. Is it declare as Private or Public, and is it in the userform class module or in a standard code module? Where it should be placed really depends on what the procedure does and what it uses, but you'll have to make sure it can be "seen" by the c_Combobox objects.
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On