Recordset to array?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
I'm using Excel 2010. I know how to put recordset into a range. I would need to put recordset into an array. This is the code I use to put it into a range:

Code:
    Set rsData = New ADODB.Recordset
    rsData.Open strSQL, strConnect, adOpenForwardOnly, adLockReadOnly, adCmdText

    If Not rsData.EOF Then
        Range("A1").CopyFromRecordset rsData
    Else
        MsgBox "No records returned.", vbCritical
    End If
And it works, returns 14 results (14 rows, 1 column). The difference should be on the last line, but how?

Code:
vSRA = rsData.GetRows(14) 'vSRA is a variant
Range("A1") = vSRA(1, 1)
gives subscript out of range, so does

Code:
vSRA = rsData.GetRows
Range("A1") = vSRA(1)
What's wrong? Do I need some to alter something else than that last row (where I copy it from the recordset to a range)?
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Also, when I want to query again, should I do

Code:
    Set rsData = Nothing
    Set rsData = New ADODB.Recordset

or is that first line needless?
 
Upvote 0
Hi

To load the results of your query into an array use :-

Code:
vSRA = rsData.GetRows()

then to find the number of records that you have retrieved -

Code:
returnedRows = UBound(vSRA, 2)

then filling the cells -

Code:
Range("A1") = vSRA(0, 0)

The records are stored starting at zero.

If you are retrieving a different set of records it is always good housekeeping to set the recordset to "Nothing".

hth

Mike
 
Upvote 0

Forum statistics

Threads
1,211,791
Messages
6,103,991
Members
447,889
Latest member
birdman15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top