Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make Recordset into Array

Status
Not open for further replies.

Kib

Programmer
May 17, 2001
58
US
Hello,
I need help converting a recordset into an array. Not sure what the best way to do this is. Thanks.
Kib
 
Why? Access is better suited for working with recordsets, so if you've got the recordset working use it! Arrays are easier to work with in VB, but not VBA. Is there some compelling reason you need to use an array?

Joe Miller
joe.miller@flotech.net
 
Set rst = CurrentDb.OpenRecordset(sSQL)
'Redim array and store headings
With rst
.MoveLast
lngFieldCount = .Fields.Count - 1
lngRecordCount = .RecordCount
ReDim vTable(0 To lngRecordCount, 0 To lngFieldCount)
.MoveFirst
Do Until .EOF
For lngFieldCounter = 0 To lngFieldCount
If lngCurrentRecord = 0 Then
vTable(lngCurrentRecord, lngFieldCounter) = .Fields(lngFieldCounter).Name
Else
vTable(lngCurrentRecord, lngFieldCounter) = .Fields(lngFieldCounter).Value
End If
Next lngFieldCounter
lngCurrentRecord = lngCurrentRecord + 1
If lngCurrentRecord > 1 Then
.MoveNext
End If
Loop
End With

I use this snippet to pack a rst into an array and then export it to Excel thru aUtomation.

With oWS
.Range(.Cells(1, 1), .Cells(lngRecordCount + 1, lngFieldCount + 1)).Value = vTable
.Columns.AutoFit
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top