Solved

redim preserve an array to a smaller amount

Posted on 2002-03-21
6
751 Views
Last Modified: 2012-06-27
Hi

Im rediming an array to a smaller amount and Im getting an error 'subscript out of range'

eg
dim arr()

'get y
redim preserve arr(y,8)

' catch that arr should be smaller so
''get  x
redim preserve arr(x,8) - this is where I get the error

any ideas??
0
Comment
Question by:louise_8
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 6886244
This is a limitation of VB.

According to the help file (for "ReDim Statement"):
"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."
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6886813
Clif's got it
0
 
LVL 18

Accepted Solution

by:
mdougan earned 20 total points
ID: 6886932
Yes, what Clif said is correct.  In your example, you can probably get away with switching the position of the dimensions:

dim arr()

'get y
redim preserve arr(8,y)

' catch that arr should be smaller so
''get  x
redim preserve arr(8,x)

Keep in mind that while the available array elements would be less, you are not actually freeing up any memory by doing this.  The only way memory would get freed for this array is if you :

Dim a temp array the size of your perm array
copy perm array to a temp array
Erase perm array
redim perm array
copy temp array to perm array
erase temp array
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 10

Expert Comment

by:Clif
ID: 6888598
If "Clif's got it" and "what Clif said is correct" then why did the grade and points go to mdougan?
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6888803
Because mdougan suggested a work around
0
 
LVL 10

Expert Comment

by:Clif
ID: 6888828
Oh.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question