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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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