PDA

View Full Version : Redim setting Subscript out of range error


ScottInTexas
06-04-2003, 03:17 PM
Goodmorning,

I run a loop to fill a dynamic array with non zero values. The array is originally sized according to the recordcount of a rec set. I only want the non-zero values so I loop through the RS and add the non-zero values to a 2 dimensional. When I'm done with that I want to resize the array to reflect the number of non-zero elements.


earlier in the function ....
Dim ValAry
.
.
.
Redim ValAry(RS.RecordCount, 1)
.
.
.
If intCount > 0 Then
Redim Preserve ValAry(intCount-1,1)
CheckData=ValAry
Else
CheckData=0
End If


In a test the record set has 16 records and there are 6 non-zero values. In another it was 16 and 7 respectively.
I just want to chop off the unused array elements. Any help would be appreciated.

raf
06-04-2003, 04:19 PM
I don't see the point in that. What do you hope to gain from it? Before the script is executed, the memoryspace for the array was reserved. It can be enlarged, but i don't believe there is something as freeing it partially.

Anyway, "if you use the preserve keyword, you can only resize the last dimension" --> not the first like in your code

more info from helpfile
-------------------------------
Description
Declares dynamic-array variables, and allocates or reallocates storage space at procedure level.
Syntax
ReDim [Preserve] varname(subscripts) [, varname(subscripts)] . . .
The ReDim statement syntax has these parts:

Part Description
Preserve Preserves the data in an existing array when you change the size of the last dimension.
varname Name of the variable; follows standard variable naming conventions.
subscripts Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:
upper [,upper] . . .

The lower bound of an array is always zero.



Remarks
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
If you use the Preserve keyword, you can resize only the last array dimension, and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)


--------------------------------------------------------------------------------

Caution If you make an array smaller than it was originally, data in the eliminated elements is lost.

--------------------------------------------------------------------------------


When variables are initialized, a numeric variable is initialized to 0 and a string variable is initialized to a zero-length string (""). A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing.

ScottInTexas
06-04-2003, 04:23 PM
Thanks for the answer. I had misunderstood the description and thought that I could shrink the array down to size. I'll just have to re think the way I use the array.

Thanks again.