Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Arrays/Redim/Help!

Working with arrays gives me vertigo!

I want to loop through a recordset and store two pieces of information in an
array, re-sizing the array with Redim Preserve as I process each new record
in the recordset. The populated array should look like this:

Thing1, .25
Thing2, 1.43
Thing3, .07
etc.

But I'm not seeing the forest for the trees -- my code throws a "subscript
out of range" on the second Redim. I've tweaked it various ways, most recent
version is

x = 0
Rs.MoveFirst
Do While Not Rs.EOF
ReDim Preserve varPriceTable(x + 1, x + 1)
varPriceTable(x, 0) = Rs!LookupString
varPriceTable(x, 1) = Rs!ZPMILEPrice
x = x + 1
Rs.MoveNext
Loop

Somebody please help me take the blinders off! ;)


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Arrays/Redim/Help!

The rule with multi dim arrays is that you can only redim the last element in
the array. Once created the first element is permanent. So code like this
will work...

Sub test()
Dim ary() As String

ReDim ary(10, 10)
ary(1, 1) = "Tada"
ReDim Preserve ary(10, 11)
End Sub

But code like this will not work
Sub test()
Dim ary() As String

ReDim ary(10, 10)
ary(1, 1) = "Tada"
ReDim Preserve ary(11, 11)
End Sub

It has to do with the way memory is stored. A multi dim array, like any
array, is stored as a big long memory string. Adding to the final element
adds another entire block to the end of the memory. Adding to the first
element(s) would require adding more memory to each of the already existing
block which it just will not do.

Have you considered use a type something like this...
'**At top of module
Type MyTpye
str as string
dbl as double
end type
'**

x = 0
Rs.MoveFirst
Do While Not Rs.EOF
ReDim Preserve varPriceTable(x+1)
varPriceTable(x).str = Rs!LookupString
varPriceTable(x).dbl = Rs!ZPMILEPrice
x = x + 1
Rs.MoveNext
Loop


--
HTH...

Jim Thomlinson


"LarryP" wrote:

Working with arrays gives me vertigo!

I want to loop through a recordset and store two pieces of information in an
array, re-sizing the array with Redim Preserve as I process each new record
in the recordset. The populated array should look like this:

Thing1, .25
Thing2, 1.43
Thing3, .07
etc.

But I'm not seeing the forest for the trees -- my code throws a "subscript
out of range" on the second Redim. I've tweaked it various ways, most recent
version is

x = 0
Rs.MoveFirst
Do While Not Rs.EOF
ReDim Preserve varPriceTable(x + 1, x + 1)
varPriceTable(x, 0) = Rs!LookupString
varPriceTable(x, 1) = Rs!ZPMILEPrice
x = x + 1
Rs.MoveNext
Loop

Somebody please help me take the blinders off! ;)


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Arrays/Redim/Help!

Had not realized that restriction on Redim. Your alternate approach appears
to work fine, many thanks.

"Jim Thomlinson" wrote:

The rule with multi dim arrays is that you can only redim the last element in
the array. Once created the first element is permanent. So code like this
will work...

Sub test()
Dim ary() As String

ReDim ary(10, 10)
ary(1, 1) = "Tada"
ReDim Preserve ary(10, 11)
End Sub

But code like this will not work
Sub test()
Dim ary() As String

ReDim ary(10, 10)
ary(1, 1) = "Tada"
ReDim Preserve ary(11, 11)
End Sub

It has to do with the way memory is stored. A multi dim array, like any
array, is stored as a big long memory string. Adding to the final element
adds another entire block to the end of the memory. Adding to the first
element(s) would require adding more memory to each of the already existing
block which it just will not do.

Have you considered use a type something like this...
'**At top of module
Type MyTpye
str as string
dbl as double
end type
'**

x = 0
Rs.MoveFirst
Do While Not Rs.EOF
ReDim Preserve varPriceTable(x+1)
varPriceTable(x).str = Rs!LookupString
varPriceTable(x).dbl = Rs!ZPMILEPrice
x = x + 1
Rs.MoveNext
Loop


--
HTH...

Jim Thomlinson


"LarryP" wrote:

Working with arrays gives me vertigo!

I want to loop through a recordset and store two pieces of information in an
array, re-sizing the array with Redim Preserve as I process each new record
in the recordset. The populated array should look like this:

Thing1, .25
Thing2, 1.43
Thing3, .07
etc.

But I'm not seeing the forest for the trees -- my code throws a "subscript
out of range" on the second Redim. I've tweaked it various ways, most recent
version is

x = 0
Rs.MoveFirst
Do While Not Rs.EOF
ReDim Preserve varPriceTable(x + 1, x + 1)
varPriceTable(x, 0) = Rs!LookupString
varPriceTable(x, 1) = Rs!ZPMILEPrice
x = x + 1
Rs.MoveNext
Loop

Somebody please help me take the blinders off! ;)


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why use redim when creating an array of arrays? Joe Dunfee Excel Programming 5 June 8th 07 04:40 PM
Working with ranges in arrays... or an introduction to arrays Glen Excel Programming 5 September 10th 06 08:32 AM
Arrays - declaration, adding values to arrays and calculation Maxi[_2_] Excel Programming 1 August 17th 06 04:13 PM
Dim and Redim Sean Excel Programming 4 June 5th 06 07:31 PM
ReDim, Preserve and Multidimensional arrays Andy Westlake[_2_] Excel Programming 3 October 19th 04 07:04 PM


All times are GMT +1. The time now is 08:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"