Currently, we are able to build reports in Access. However, they want that same report built in Excel. These reports are regulated and need to be designed to specifications. I have designed the Excel template(workbook) with one tab having the Report and another tab having the data. Desired outcome: User would click a button to export the data. Open the pre-designed Excel template, populate the data tab, save the Excel file with specific name in predefined directory, view the report on the report tab. Below is what I have constructed so far and is only able to create a new file not use the pre-designed Excel Template. Actually, this code does not work right now. Any suggestions or tutelage would be greatly appreciated. :-)
Private Sub Button_ExhP2_XLSX_Click()
DoCmd.SetWarnings False
Set db = CurrentDb
Dim mysql As String
Set fso = CreateObject("Scripting.FileSystemObject")
'Create filepath for data to be saved in
Const xlsxPath = "\\Path\Access dBase\O-Blank Exhibits\"
'Create Filname used to save data to excel
FileName = "BlankPart2 " & [Forms]![Frm_Main]![Frm_Main_ListBox_BU] & "_" & [Forms]![Frm_Main]![Frm_Main_Combo_Qtr] & "_" & [Forms]![Frm_Main]![Frm_Main_Combo_Year]
'Delete file if it already exists so it may fully be replaced with new data
If fso.FileExists(xlsxPath & FileName & ".xlsx") Then
Kill xlsxPath & FileName & ".xlsx"
End If
'Pull detail based on Part 2
mysql = ""
mysql = mysql + " SELECT "
mysql = mysql + " BSTimePeriod, "
mysql = mysql + " ISTimePeriod, "
mysql = mysql + " Fiscal_Quarter, "
mysql = mysql + " Year, "
mysql = mysql + " BU, "
mysql = mysql + " BU_Description, "
mysql = mysql + " OU, "
mysql = mysql + " OU_Description, "
mysql = mysql + " P2_Category, "
mysql = mysql + " P2_Line, "
mysql = mysql + " Comp, "
mysql = mysql + " FEP, "
mysql = mysql + " Med, "
mysql = mysql + " CHP, "
mysql = mysql + " FHP, "
mysql = mysql + " Total, "
mysql = mysql + " Order, "
mysql = mysql + " Balance, "
mysql = mysql + " FROM dbo_P2_Exh_Analytic_Rpt "
mysql = mysql + " WHERE (((dbo_P2_Exh_Analytic_Rpt.Fiscal_Quarter)='" & [Forms]! [Frm_Main]![Frm_Main_Combo_Qtr] & "') "
mysql = mysql + " AND ((dbo_P2_Exh_Analytic_Rpt.Year)='" & [Forms]![Frm_Main]![Frm_Main_Combo_Year] & "') "
mysql = mysql + " AND ((dbo_P2_Exh_Analytic_Rpt.BU)='" & [Forms]![Frm_Main]![Frm_Main_ListBox_BU] & "')) "
DoCmd.RunSQL mysql
'Open record set create for Part 2
Set rst = db.OpenRecordset(mysql, dbOpenDynaset)
'export summary detail file to excel
DoCmd.TransferSpreadsheet acExport, 10, "Orange Part 2", xlsxPath & FileName & ".xlsx", True
Set db = Nothing
DoCmd.SetWarnings True
MsgBox ("BlankPart 2 data has been exported.")
'additional comments: error may occur if Category doesn't exist in dbo.MTest_Sums, didn't build error trap because wasn't sure if this could happen
End Sub
The error is: Compile Error: Variable Not found
Set db = CurrentDb
is highlighted