How to include headers from recordset

dboone25

Board Regular
Joined
May 8, 2015
Messages
185
Good morning all. Having a few issues with including the headers from a SQL query dataset. The dataset is copied from a databse into an activesheet but I cant seem to include the headers..

My code below:

Code:
Private Sub CommandButton1_Click()

        'Declare variables'
        Dim objMyConn As ADODB.Connection
        Dim objMyCmd As ADODB.Command
        Dim objMyRecordset As ADODB.Recordset


        Set objMyConn = New ADODB.Connection
        Set objMyCmd = New ADODB.Command
        Set objMyRecordset = New ADODB.Recordset


        'Open Connection'
        objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=NP-DATABASE;Initial Catalog=" & ComboBox1.Value & " ;Integrated Security=SSPI;"


        objMyConn.ConnectionTimeout = 0
        objMyConn.CommandTimeout = 0


        objMyConn.Open


        'Set and Excecute SQL Command from TextBox'
        Set objMyCmd.ActiveConnection = objMyConn
        objMyCmd.CommandText = TextBox1.Value  ' query run from TextBox
                              
        objMyCmd.CommandType = adCmdText


       
       'include headers from recordset
       With objMyRecordset
        For i = 1 To .Fields.Count
            ActiveSheet.Cells(1, i) = .Fields(i - 1).Name
        Next i
        
       End With
        
       
       
       


        'Open Recordset'
        Set objMyRecordset.Source = objMyCmd
        objMyRecordset.Open


        'Copy Data to Excel'
        ActiveSheet.Range("A1").CopyFromRecordset objMyRecordset


End Sub

Appreciate any help with this...many thanks :-)
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You're trying to output the headers before you open the recordset, and you then start the data output over the top of the headers anyway. Try this instead:
Rich (BB code):
'Open Recordset'
        Set objMyRecordset.Source = objMyCmd
        objMyRecordset.Open

       'include headers from recordset
       With objMyRecordset
        For i = 1 To .Fields.Count
            ActiveSheet.Cells(1, i) = .Fields(i - 1).Name
        Next i
        
       End With
        'Copy Data to Excel'
        ActiveSheet.Range("A2").CopyFromRecordset objMyRecordset


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,228
Members
453,025
Latest member
Hannah_Pham93

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