graviz
Registered User.
- Local time
- Today, 01:40
- Joined
- Aug 4, 2009
- Messages
- 167
I have this code to output a query to an Excel spreadhseet and I'm trying to output the headings of the query as well. Can anyone tell when what I need to do in my code to make this happen?
Public Function ExpExcel()
'Create connection to Database
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
'Variables to refer to Excel and Objects
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set cnn = CurrentProject.Connection
MyRecordset.ActiveConnection = cnn
'SQL statements to extract the data required for the report - From a query
MySQL = "SELECT * From "
MySQL = MySQL & "01_Created_Jobs"
MyRecordset.Open MySQL
' Tell it location of actual Excel file
MySheetPath = "\\Mer2-corpfs1\dnsc\Resource Management\Slides\Friday Slide Prep\Bernie Data\Bernie Prep Template.xls"
'Open Excel and the workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure excel is visible on the screen
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets("Sheet1")
'Insert the Recordset in the excel sheet starting at specified cell
XlSheet.Range("A1").CopyFromRecordset MyRecordset
'Clean up and end with worksheet visible on the screen
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
XlBook.Save
XlBook.Close
Set XlBook = Nothing
Set XlSheet = Nothing
End Function
Public Function ExpExcel()
'Create connection to Database
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
'Variables to refer to Excel and Objects
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set cnn = CurrentProject.Connection
MyRecordset.ActiveConnection = cnn
'SQL statements to extract the data required for the report - From a query
MySQL = "SELECT * From "
MySQL = MySQL & "01_Created_Jobs"
MyRecordset.Open MySQL
' Tell it location of actual Excel file
MySheetPath = "\\Mer2-corpfs1\dnsc\Resource Management\Slides\Friday Slide Prep\Bernie Data\Bernie Prep Template.xls"
'Open Excel and the workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure excel is visible on the screen
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets("Sheet1")
'Insert the Recordset in the excel sheet starting at specified cell
XlSheet.Range("A1").CopyFromRecordset MyRecordset
'Clean up and end with worksheet visible on the screen
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
XlBook.Save
XlBook.Close
Set XlBook = Nothing
Set XlSheet = Nothing
End Function