views:

59

answers:

1

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?

Edited to add

formTab was declared as a Control, but I find that if it is declared as an Object then the code works.

This will probably solve my problem, but in the interests of furthering my knowledge I would be grateful for any explanation of this behaviour, particularly with regard to the difference in running the code in the different types of module.

+4  A: 

This is a really great question. Your edit at the end of the post reveals a lot about how VBA works and what's going on here. I'm not 100% this what's going on, but I'll explain what I think is happening.

A Collection in VBA (and VB6, for that matter; same code base) is not strongly typed. This means that everything in a collection is technically an "object." In the .NET world (as of .NET 2.0), it's possible to have strongly typed collections so that you could say "everything in this collection is a Control object." In VBA, this isn't possible with a Collection.

In your first iteration, where you are referring to the item indexed in the activeTabs collection, activeTabs(x + 1) is referring to an object. When you tell VBA to look up .Caption of that object, it doesn't know what the underlying type is (I think), so it has to simply look to see if the underlying object type contains a property or method called Caption. As you can see, Tab controls do in fact contain a property called Caption.

In your second interation, where you are doing a For Each loop, I think the problem is that the Control type probably doesn't have a property called Caption, though different types of controls probably do. For example, a text box control probably doesn't have a Caption property whereas as label control does have a Caption property.

You have a few options to fix your second loop. 1) You could declare formTab as a Tab control (I'm not sure exactly what it's called). The Tab control should have a Caption property. 2) If every control in activeTabs is not specifically a Tab control (in which case, you should probably call it activeControls instead of activeTabs), you could check within your loop to see if the formTab is actually a Tab control. If it is, cast it as a Tab control and then call .Caption. Until you cast it as a Tab control, VBA won't know that it has a Caption property since a regular Control object doesn't have a caption property.

In the end, you can get away with using objects as in your first loop and letting the runtime figure out what to do, but that can give really bad performance. In general, it's better to work with your specific types in a strongly-typed language. It also helps to show in your code that you know specifically what you're working with rather than leaving it to the runtime to decide what properties and methods you can work with.

Ben McCormack
Thanks, this makes a lot of sense. I declared formTab as Page (the correct name for the control, I just prefer calling them tabs) and as you predict it works just fine. I also copied the code into a form module and there it works regardless of whether formTab is declared as a Control, Page or Object. Weird.
Lunatik
@Lunatik VB sometimes will jump through a lot of hoops to try to figure out what you're trying to do. Unfortunately, this doesn't always lead to good code and will let you get away with not really knowing what your code is doing. I've written a ton of code where I didn't know what it was doing but VBA allowed it to work :-).
Ben McCormack