ADODB RecordSet Question = Row Headers?

bblevins

New Member
Joined
Nov 3, 2008
Messages
22
Hi All,

I um using a UserForm to build dynamic queries based on user input. Based on what the user selections are, the columns returned can be different. There will be anywhere from 1 to 4 columns returned. Once the query is built, I have the following function that will write the results of the query into an array "arrData". This all works as expected. Once I have the data in the array I need to output it to another sheet. The only thing missing is that I cannot figure out how to get the column names into the RecordSet/Array. This has to be possible, but I have been unable to figure it out.

An example query would be as follows:
SQL:
SELECT DISTINCT [MP], [CE], [Vendor], [Store Name] FROM [External Views].[dbo].[Performance Reporting Scorecard Wireless Monthly]

So in this instance, I want to see the column names for each of the four columns in the first row of each array column. Thoughts?

VBA Code:
Public Function GetData(strSQL As String)

Dim arrData() As Variant
Dim Conn As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim DBPath As String, sconnect As String

'Establish the connection
    If Conn.State <> 1 Then
        Conn.Open "Provider=SQLNCLI11.1;Password=MyPassword;Persist Security Info=True;User ID=MyUser;Initial Catalog=External Views;Data Source=xxxxxxxxxxxx;"
    End If
    

'Open the recordset
    RS.Open strSQL, Conn

    
'Load the Data into an array
    arrData= RS.GetRows
     
    
'Close Recordset
    RS.Close

'Close Connection
    Conn.Close

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
OK, I do see that I can access the field names "Headers" like this:

VBA Code:
For i = 0 To rs.Fields.Count - 1
    Cells(1, i + 1).Value = rs.Fields(i).Name
Next


So as far as identifying what columns are there that handles it. Perhaps this is all I need. Then I can locate the correct column in the destination worksheet for that column of data from the array and dump the data in the correct column onto the worksheet. Any way to dump 1 entire column of the array on the worksheet without looping through? I need to do it column by column because my data is simply serving as a ref lookup in formulas elsewhere on the page. So I need them to be in the right place every time.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

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