Collection(0) Out of Range

J

Jack Leach

I just got done setting up a function that recursively calls all the controls
in a commandbar and sets the visiblilty for each control.

In order to do this I had to trap an error by checking the Controls
collection of each control in the menu. The error, btw, is 438: Object does
not support this property or method.

Here's the line I check it with:

Set cbarc = cbar.Controls(1)

which correctly returns the first nested item, or error 438. No problem.

My question is this: every other array that I've used is 0 based. The
following produces error 9: Subscript out of range

Set cbarc = cbar.Controls(0)

....regardless if there's further nested controls or not.

I even put Option Base 0 in the module declarations, with the same result.

Not a big deal, but I'm just curious why this is the only collection I've
run into that is 1 based rather than 0 based. Is there some set of rules for
which collections and/or arrays are always this way, regardless of Option
Base 0?

Are Collections different from Arrays in that they will always be 1-based
rather than 0?

I thought I've used collections before by using 0, so I'm a bit perplexed...


just curious... thanks!
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
B

Banana

Unfortunately, the implementation has been inconsistent. Any
user-defined collections, the base is one, not zero, while built-in
collections & arrays are zero. This has been that way AFAIK.

Option Base 0 does not change the definition of preexisting collections
& arrays; only user-defined arrays will pick up the Option Base, IINM.
(I can't remember if the user-defined collection respect the Option
Base. Check VBA help files on that one.)
 
J

Jack Leach

Any
user-defined collections, the base is one, not zero, while built-in
collections & arrays are zero.

I would assume the Controls collection of the CommandBars collection and
it's nested controls would be considered built-in rather than user defined.

Option Base 0 does not change the definition of preexisting collections
& arrays

I had always been under the impression Base 0 forces all (arrays, at least)
to a 0 base regardless of the default.

Sadly, the documented help files for the Office Controls collection or the
Option Base 0 give no indications of these details.


No biggie, I'll just scrap the idea that Base 0 will solve any of these
problems (not that I run into them often... this would be the first time...
it seems safe enough to plan on a 0-base, and you will be let known if you're
wrong <g>)

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
B

Banana

Jack said:
I would assume the Controls collection of the CommandBars collection and
it's nested controls would be considered built-in rather than user defined.

That's true, but I remember running into same thing myself and just
chalked it up to "inconsistent implementation"
Sadly, the documented help files for the Office Controls collection or the
Option Base 0 give no indications of these details.

Just to be sure, though, did you check VBA editor's help files, not
Access help files? I've observed that same question asked in either
search bar can give different answers and most of time, VBA's help files
are more relevant to programmers.
you will be let known if you're
wrong <g>)

Bring it on. I learn far more when I'm wrong than when I'm right! <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top