Bob Larson helped me greatly with his code to go from Access to Excel. Everything works great and all the data is populated on one Excel tab.
Now, I'm trying to add a second Excel tab to that same spreadsheet if a user picks a certain report from a combo box.
Here is the code--the bottom part (highlighted in red) is where I am stuck.
'---------------------------------------------------------------------------------------
' Procedure : Send2Excel
' Author : Bob Larson
' Date : 5/25/2008
' Purpose : Send any single recordset form to Excel. This will not work with
' subforms.
' Use : You may freely use this code as long as the author information in
' this header remains intact
'---------------------------------------------------------------------------------------
Public Sub Send2Excel(qry As String, Optional strSheetName As String)
' qry is the name of the query you want to send to Excel
' strSheetName is the optional name of the sheet you want to name it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs(qry)
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
'check to see if there is data. If not, display a message and exit the function
If rst.RecordCount = 0 Then
MsgBox "Your report selection returned no data", , "No data"
Exit Sub
End If
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Add
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Sheet1")
If Len(strSheetName) > 0 Then
xlWSh.Name = Left(strSheetName, 34)
End If
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
'delete Sheet2 and Sheet3
xlWBk.Sheets(Array("Sheet2", "Sheet3")).Select
xlWBk.Sheets("Sheet3").Activate
ApXL.ActiveWindow.SelectedSheets.Delete
'name the tab with the report name
ApXL.Sheets("Sheet1").Name = Forms!frmEscalationReports!cboEscalation.Column(1)
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
'*************for users that select Report ID=13 *************************
'if user selects Report ID=13, then need a new tab.
If Forms!frmEscalationReports!cboEscalation.Column(0) = 13 Then
ApXL.Sheets.Add
ApXL.Sheets("Sheet4").Select
ApXL.Sheets("Sheet4").Move After:=ApXL.Sheets(ApXL.Worksheets.Count)
ApXL.Sheets("Sheet4").Select
ApXL.Sheets("Sheet4").Name = "Codes by Category"
End If
'the new tab should contain the data in strSQL
Dim strSQL As String
strSQL = "SELECT tblEscalationReportsDetail.[Criteria Code], [KPI Rating Criteria].[Criteria Description], tblEscalationReportsDetail.CodeGrouping AS Category " & _
"FROM tblEscalationReportsDetail INNER JOIN [KPI Rating Criteria] ON tblEscalationReportsDetail.[Criteria Code] = [KPI Rating Criteria].[Criteria Code] " & _
"WHERE (((tblEscalationReportsDetail.ReportIDLink)=13)); "
'This is where I get stuck. I created and named this new tab. Now how do I populate it with the data from strSQL above?
rst.Close
Set rst = Nothing
Exit Sub
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Sub
End Sub
Thanks!
Now, I'm trying to add a second Excel tab to that same spreadsheet if a user picks a certain report from a combo box.
Here is the code--the bottom part (highlighted in red) is where I am stuck.
'---------------------------------------------------------------------------------------
' Procedure : Send2Excel
' Author : Bob Larson
' Date : 5/25/2008
' Purpose : Send any single recordset form to Excel. This will not work with
' subforms.
' Use : You may freely use this code as long as the author information in
' this header remains intact
'---------------------------------------------------------------------------------------
Public Sub Send2Excel(qry As String, Optional strSheetName As String)
' qry is the name of the query you want to send to Excel
' strSheetName is the optional name of the sheet you want to name it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs(qry)
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
'check to see if there is data. If not, display a message and exit the function
If rst.RecordCount = 0 Then
MsgBox "Your report selection returned no data", , "No data"
Exit Sub
End If
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Add
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Sheet1")
If Len(strSheetName) > 0 Then
xlWSh.Name = Left(strSheetName, 34)
End If
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
'delete Sheet2 and Sheet3
xlWBk.Sheets(Array("Sheet2", "Sheet3")).Select
xlWBk.Sheets("Sheet3").Activate
ApXL.ActiveWindow.SelectedSheets.Delete
'name the tab with the report name
ApXL.Sheets("Sheet1").Name = Forms!frmEscalationReports!cboEscalation.Column(1)
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
'*************for users that select Report ID=13 *************************
'if user selects Report ID=13, then need a new tab.
If Forms!frmEscalationReports!cboEscalation.Column(0) = 13 Then
ApXL.Sheets.Add
ApXL.Sheets("Sheet4").Select
ApXL.Sheets("Sheet4").Move After:=ApXL.Sheets(ApXL.Worksheets.Count)
ApXL.Sheets("Sheet4").Select
ApXL.Sheets("Sheet4").Name = "Codes by Category"
End If
'the new tab should contain the data in strSQL
Dim strSQL As String
strSQL = "SELECT tblEscalationReportsDetail.[Criteria Code], [KPI Rating Criteria].[Criteria Description], tblEscalationReportsDetail.CodeGrouping AS Category " & _
"FROM tblEscalationReportsDetail INNER JOIN [KPI Rating Criteria] ON tblEscalationReportsDetail.[Criteria Code] = [KPI Rating Criteria].[Criteria Code] " & _
"WHERE (((tblEscalationReportsDetail.ReportIDLink)=13)); "
'This is where I get stuck. I created and named this new tab. Now how do I populate it with the data from strSQL above?
rst.Close
Set rst = Nothing
Exit Sub
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Sub
End Sub
Thanks!