Excel VBA - RecordSet To Array

Harry Flashman

Active Member
Joined
May 1, 2011
Messages
361
I have an SQL Query which returns a subset of data from one worksheet to another worksheet. I was hoping that instead of returning the data to a worksheet I might be able to return the data to an array instead.

Here is my query:
Code:
Dim sel As New cls_Selected

Sub Data_Ext_From_Excel()

'Application.ScreenUpdating = False
Dim ws As Worksheet
Dim MyConnect As String
Dim MyRecordSet As ADODB.Recordset
Dim MySQL As String
Dim fName As String, fPath As String, dSource As String

fName = ThisWorkbook.Name
fPath = ThisWorkbook.Path
dSource = fPath & "\" & fName

MyConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & dSource & ";" & _
            "Extended Properties=Excel 12.0"
            
Dim strCat As String
Dim strGroup As String

strCat = sel.Category ' Selected member from ListBox
strGroup = sel.Group ' Selected member from ListBox

MySQL = "SELECT [Category], [Group], [Item], [Letter] FROM [Data$] " & _
"WHERE [Category] = " & "'" & strCat & "'" & " AND [Group] = " & "'" & strGroup & "'"
Debug.Print MySQL
Set MyRecordSet = New ADODB.Recordset

    MyRecordSet.Open MySQL, MyConnect, adOpenForwardOnly, adLockReadOnly
   
    'clear existing data on the sheet
    Set ws = Sheets("Data_Fetch")
    ws.Cells.Clear
  
    'create field headers
    i = 0
    With ws.Range("A1")
       For Each fld In MyRecordSet.Fields
           .Offset(0, i).Value = fld.Name
           i = i + 1
       Next fld
    End With
     
    'transfer data to Excel
    Dim arr

    ws.Range("A2").CopyFromRecordset MyRecordSet
    ' The next line returns an error message
    ' Error = Either BOF or EOF is true, or the current record has been deleted.
    ' arr = MyRecordSet.GetRows


   
    ' Close the connection
    MyRecordSet.Close

    Set MyRecordSet = Nothing

    
End Sub

I have done some googling and I found code that uses the .GetRows property, but this throws up an error message in my code.

Can anyone see where I going wrong? Is there a way that I can pass the recordset straight to an array rather than outputting the data to a worksheet.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,213,510
Messages
6,114,048
Members
448,543
Latest member
MartinLarkin

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