
Defining an array and getting Subscript Out of Range Error
Debbie,
I know that you have fixed your difficulty, but I can give you an
alternative approach which would avoid having to pre-set the size of the
array. Which approach one uses, is I think, really a matter of personal
preference - not much difference in efficiency either way, AFAIK.
A further point before I get the alternative code: Re pre-sizing (?) the
array, you could hard code it to initially be, lets say a size of 100, ie
100 possible elements (0 to 99) - far larger than you actually need. Then
populate the array, keeping track of the last array member used. Then do
redim preserve arryFred(count + 1) - I think the syntax is correct, but I
haven't checked it.
The other way you could handle the array building is:
dim vArrayContents as Variant
dim i as Integer
vArrayContents = Array("........", "..........", "...........",
".............")
Then, you could treat vArrayContents as an array - thereby achieving your
initial requirement. Also, as an extra benefit, if you want to add a new
array member, than all you need to do is add the new item to the above array
setup and you're finished :-)
eg. Of usage of this variant array
For i = LBound(vArrayContents) To UBound(vArrayContents)
.....
.....
Next
As to how the code you inherited worked, did it have an Option Explicit line
at top of the code module? - if not then the array could have *maybe* been
built on the fly without having bounds checking performed [this is just a
theory - sounds good in any case :-)]
HTH,
Rob.
Quote:
> I figured it out. The array hadn't been declared in the version of the
> template I inherited, so I'm curious how it even worked. Anyway, I added:
> Dim arrTypeOfBoard(23) As String
> This resolved the problem. It seems kind of counter-productive to have to
> define the number of items in an array.
> Thanks for chiming in.
> Debbie
> > Debbie
> > A couple of ideas:
> > How have you defined the array and also said how big it will be? Also,
> does
> > your code contain, in general|declarations section of the module, Option
> > Base 1?
> > Rob.
> > > So, I have a working template created by someone else. All I needed
to
> do
> > > was add five additional items into an array which populates a
combobox.
> I
> > > added them, but now I get the error "Subscript out of range" when the
> > system
> > > executes the first item in the array statement. When I hover over the
> > > offending statement
> > > arrTypeOfBoard(0) = "CLA - Claims, Collections & Litigation Matters"
> > > the tooltip says:
> > > arrTypeOfBoard(0) = [subscript out of range]
> > > What would be causing that error? I haven't changed any other part of
> the
> > > code and it functions fine without the addition of my five entries.
> > > Thanks in advance for any help.
> > > Debbie