I get a subscript out of range error on a redim of a two-dimensional array. Can anyone help, the code(at least a part of it) follows:
dim os() as string
redim os(0, 1)
//I get a document collection from a view and loop through it
do until doc is nothing
//First time round when the array is empty just add the values to the first line of the array
If os(0, 0) = “” then
os(0, 0) = doc.username(0)
os(0, 1) = doc.otherfield(0)
Else
upper = ubound(os, 1) + 1 //is 1 the second time round, as expected
redim preserve os(upper, 1) //Error occurs
//More code follows, including the loop
I’m just trying to up the first dimension to have an extra username, with two values.
What’s the problem with this, I’ve had a search of the forum and couldn’t find anything, so any help would be great.
Thanks
Dan
Subject: You can only redim the last dimension in a multi dimensioned array…
dim os() as stringredim os(1, 0)
//I get a document collection from a view and loop through it
do until doc is nothing
//First time round when the array is empty just add the values to the first line of the array
If os(0, 0) = “” then
os(0, 0) = doc.username(0)
os(1, 0) = doc.otherfield(0)
Else
upper = ubound(os, 2) + 1 //is 1 the second time round, as expected
redim preserve os(1, upper) //Error will not occur
Subject: RE: You can only redim the last dimension in a multi dimensioned array…
Bill
Thanks for that, took me a while to get my head around, but it’s working as you say.
What confused me, and maybe you can shed some light on it, is that designer help says:
Arrays can have up to 8 dimensions. The first ReDim statement for an array sets the number of dimensions for the array. Subsequent ReDim statements for the array can change the upper and lower bounds for each dimension, but not the number of dimensions.
This seems to infer that you can change any dimension, not just the last one.
Cheers
Dan
Subject: You can change the bounds of each dimension EXCEPT…
You have to look one more line down…Arrays can have up to 8 dimensions. The first ReDim statement for an array sets the number of dimensions for the array. Subsequent ReDim statements for the array can change the upper and lower bounds for each dimension, but not the number of dimensions.
If Preserve is specified, you can change only the upper bound of the last array dimension. Attempting to change any other bound results in an error.
Subject: RE: You can change the bounds of each dimension EXCEPT…
That’ll be it then!
Thanks a lot, eveything’s a lot clearer now
Dan