# Thread: Populating 3 Dimension Array

1. ## Populating 3 Dimension Array

My brain is toast today. I used to know how to do this. I have a recordset with 3 fields, and need to store them in an array. If I have, say 10 records, how do I declare the array and populate it with the field's data?

Thanks,
James

2. This is quick and dirty

```Dim RowBuf As Variant
Dim DisRS As rdoResultset
Set DisRS = cn.OpenResultset(SQL, rdOpenForwardOnly)
RowBuf = DisRS.GetRows(50)```

3. Here is something I threw together, there is probably a better way.

```Option Base 1
Option Explicit
Sub Test()
Dim x As Long
Dim y As Long
Dim z As Long
Dim MyArrays(10, 10, 10) As String
Dim MyCol As Long
Dim MyRow As Long
MyCol = 1
MyRow = 1
For x = 1 To 10
For y = 1 To 10
For z = 1 To 10
MyArrays(x, y, z) = Cells(MyRow, MyCol).Text
If MyCol = 3 Then
MyCol = 1
Else
MyCol = MyCol + 1
End If
If MyRow = Range("A65536").End(xlUp).Row Then
GoTo z:
Else
MyRow = MyRow + 1
End If
Next z
Next y
Next x
z:
End Sub```
If you had data in A1:C something then this would fill A1:C1. then A2:C2 etc. into the array.

But this data is hard to visualize (for me at least). If I had three record sets, I would use 3 seperate Arrays to fill the data.

4. Hi Jacob,

Good to see that You challenge arrays

If we don't insist to use an array of a specific datatype then I would prefer to use the following approach:

```Option Explicit
Sub Variant_Array()
Dim wsSheet As Worksheet
Dim rnData As Range
Dim i As Long, j As Long, k As Long
Set wsSheet = ActiveSheet
With wsSheet
Set rnData = .Range(.Range("A1"), .Range("C65536").End(xlUp))
End With
'Read all data into the array.
'Variant-arrays that reads data from a range is always 1-based.
'Here we make a dummy addition just to loop through the array:
For j = 1 To 3
For i = 1 To UBound(vaData)
Next i
Next j
'Read the revised data back to the range.
End Sub```

Hi Tommy,

Long time since I saw a RDO-approach as most people nowdays prefer ADO

Thank you for reading my post on this subject,
Dennis

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•