Include SQL headers into New WorkBook

dboone25

Board Regular
Joined
May 8, 2015
Messages
185
Hi. Little stuck on this one.

I have a script that reads an SQL query and outputs the information into a new excel workbook. The problem i am having is getting the headers from the same script into the new workbook.

Code:
'Declare variables'    Set objMyConn = New ADODB.Connection
    Set objMyCmd = New ADODB.Command
    Set objMyRecordset = New ADODB.Recordset


'Open Connection'
    objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=np-2;Initial Catalog=TESTDB;Integrated Security=SSPI;"
    objMyConn.Open


'Set and Excecute SQL Command'
    Set objMyCmd.ActiveConnection = objMyConn
    objMyCmd.CommandText = "select * from table"
    objMyCmd.CommandType = adCmdText
    objMyCmd.Execute


'Open Recordset'
    Set objMyRecordset.ActiveConnection = objMyConn
    objMyRecordset.Open objMyCmd
    
'Loop Names'
    ' Loop to include SQL headers
    Dim i As Integer


'Copy Data to Excel'
    Workbooks.Add.Worksheets("Sheet1").Range("A2").CopyFromRecordset (objMyRecordset)
    
    For i = 0 To objMyRecordset.fields.count - 1
        Sheet1.Cells(1, i + 1) = objMyRecordset.fields(i).Name
    Next i


objMyRecordset.Close
objMyConn.Close
Set objMyRecordset = Nothing
MsgBox "Complete", vbOKOnly, "Finished"




End Sub


My eyes are starting to hurt now.

When I change the 4 loop to include Workbooks.Add this opens up 8 different workbooks. Any help on this one?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
In your code Sheet1 here will refer to Sheet1 in the workbook the code is in, it won't refer to any sheet in the workbook you added.
Code:
        Sheet1.Cells(1, i + 1) = objMyRecordset.fields(i).Name
Try replacing Sheet1 with ActiveWorkbook.Sheets("Sheet1").
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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