Solved

redim preserve an array to a smaller amount

Posted on 2002-03-21
6
703 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now