Solved

redim preserve an array to a smaller amount

Posted on 2002-03-21
6
859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

717 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