Recordset to array?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
891
Office Version
  1. 2016
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 is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
891
Office Version
  1. 2016
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?
 

ukmikeb

Well-known Member
Joined
Jul 10, 2009
Messages
2,757
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,714
Messages
5,833,281
Members
430,201
Latest member
Deepakpilla36

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
Top