Why can't I get properties from members of this collection?

Posted by Lunatik on Stack Overflow See other posts from Stack Overflow or by Lunatik
Published on 2010-04-30T11:10:50Z Indexed on 2010/04/30 11:17 UTC
Read the original article Hit count: 230

Filed under:
|
|

I've added some form controls to a collection and can retrieve their properties when I refer to the members by index.

However, when I try to use any properties by referencing members of the collection I see a 'Could not set the ControlSource property. Member not found.' error in the Locals window.

Here is a simplified version of the code:

'Add controls to collection'
For x = 0 To UBound(tabs)
    activeTabs.Add Item:=Form.MultiPage.Pages(Val(tabs(x, 1))), _
        key:=Form.MultiPage.Pages(Val(tabs(x, 1))).Caption
Next x

'Check name using collection index'
For x = 0 To UBound(tabs)
    Debug.Print "Tab name from index: " & activeTabs(x + 1).Caption
Next x

'Check name using collection members'
For Each formTab In activeTabs
    Debug.Print "Tab name from collection: " & formTab.Caption
Next formTab

The results in the Immediate window are:

Tab name from index: Caption1
Tab name from index: Caption2
Tab name from collection: 
Tab name from collection: 

Why does one method work and the other fail?

This is in a standard code module, but I have similar code working just fine from within form modules. Could this have anything to do with it?

© Stack Overflow or respective owner

Related posts about vba

Related posts about excel-vba