hockeyfan21
Registered User.
- Local time
- Today, 10:52
- Joined
- Aug 31, 2011
- Messages
- 38
Hi there, I've looked at many posts where they are using blnHeaderRow = True but I can't get it to work for me. No errors, just won't bring over the headers from my Access query. I've tried placing the line everywhere in my code with no success. Any ideas as to where i might be going wrong here?
Thank you!
Toni
Private Sub Command0_Click()
'Private Sub ToExcel_Click()
'declare variables
Dim Distinct As DAO.Recordset
Dim NeedComments As DAO.Recordset
Dim sSql As String
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
Dim r As Excel.Range
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
blnEXCEL = False
Const path As String = "C:\Users\THART2\"
'get a recordset of distinct Team names
sSql = "SELECT DISTINCT Team FROM Query1"
Set Distinct = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot)
blnHeaderRow = True
'Open Excel and make it visible
Set xlApp = New Excel.Application
xlApp.Visible = True
'Step through each Team
While Not Distinct.EOF
'Get the records associated with this Team
sSql = "SELECT ID, [Initiative Nm], [Lnch Date], [Mat Nbr],[Mat Desc],[Distrib Mthd],[Qty on order],[Launch Comments],Team,Comments FROM Query1 WHERE Team='" & Distinct(0) & "'"
'Debug.Print sSql
Set NeedComments = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot)
'Create a new workbook
Set xlWb = xlApp.Workbooks.Add()
Set xlWs = xlWb.ActiveSheet
'Get the range to paste into
Set r = xlWs.Range("A1")
'Copy to excel
r.CopyFromRecordset NeedComments
'Name the worksheet
xlWs.Name = Distinct(0)
'move to the next Team
'Save the workbook
xlWb.SaveAs path & Distinct(0) & " " & Format(Now(), "MMDDYY")
'close the workbook
' xlWb.Close False
Distinct.MoveNext
Wend
'Quit excel
xlApp.Quit
End Sub
Thank you!
Toni
Private Sub Command0_Click()
'Private Sub ToExcel_Click()
'declare variables
Dim Distinct As DAO.Recordset
Dim NeedComments As DAO.Recordset
Dim sSql As String
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
Dim r As Excel.Range
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
blnEXCEL = False
Const path As String = "C:\Users\THART2\"
'get a recordset of distinct Team names
sSql = "SELECT DISTINCT Team FROM Query1"
Set Distinct = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot)
blnHeaderRow = True
'Open Excel and make it visible
Set xlApp = New Excel.Application
xlApp.Visible = True
'Step through each Team
While Not Distinct.EOF
'Get the records associated with this Team
sSql = "SELECT ID, [Initiative Nm], [Lnch Date], [Mat Nbr],[Mat Desc],[Distrib Mthd],[Qty on order],[Launch Comments],Team,Comments FROM Query1 WHERE Team='" & Distinct(0) & "'"
'Debug.Print sSql
Set NeedComments = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot)
'Create a new workbook
Set xlWb = xlApp.Workbooks.Add()
Set xlWs = xlWb.ActiveSheet
'Get the range to paste into
Set r = xlWs.Range("A1")
'Copy to excel
r.CopyFromRecordset NeedComments
'Name the worksheet
xlWs.Name = Distinct(0)
'move to the next Team
'Save the workbook
xlWb.SaveAs path & Distinct(0) & " " & Format(Now(), "MMDDYY")
'close the workbook
' xlWb.Close False
Distinct.MoveNext
Wend
'Quit excel
xlApp.Quit
End Sub