Hi, I am trying to create a module that exports data for every store ID in an Access database to individual store excel files. I have come close, so I think, to solving the problem; however, the export string table is not being allowed to export to excel. May someone help me solve my mistake please?
It is my first time writing code in VBA and am trying my best but this one has me stuck for about 4 hours now.
here is the code:
Sub exportspreadsheet()
On Error GoTo HandleError
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strSQL2 As String
Dim strFileName As String
Dim objXLApp As Object
Set objXLApp = CreateObject("Excel.Application")
Dim objXLBook As Excel.Workbook
conPath = "C:\Users\VasquezJr\Documents\"
On Error GoTo ExportReport_Error
strSQL = "Select Distinct Final_all.Store_ID From Final_all;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
rs.MoveLast
rs.MoveFirst
Do Until rs.EOF
' create a workbook from the template
Set objXLApp = New Excel.Application
Set objXLBook = objXLApp.Workbooks.Open(conPath & "MyTemplate.xltx")
objXLBook.SaveAs (conPath & "Store_" & rs!Store_ID & "_CPC_Report.xls")
objXLBook.Close
strSQL2 = " SELECT Final_all.Store_ID, Final_all.StorePC, Final_all.FSA, Final_all.[Delivery Mode], Final_all.[Abbreviated Name], Final_all.TOTAL, Final_all.Distance_km, Final_all.MaxOfRank, Final_all.Cumm_TOT " _
& "FROM Final_all " _
& "WHERE Final_all.Store_ID =" & rs!Store_ID _
& " ORDER BY Final_all.ID, Final_all.Store_ID, Final_all.StorePC;"
strFileName = [rs]![Store_ID]
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, strSQL2, strFileName, True
rs.MoveNext
strFileName = ""
strSQL2 = ""
Set tmpLocalTable = Nothing
Set tmpLocalQuery = Nothing
Loop
On Error GoTo 0
Exit Sub
ExportReport_Error:
MsgBox "Error " & Err.Number & "(" & Err.Description & ") in procedure ExportReports"
HandleError:
Select Case Err.Number
Case 3265
Resume Next
Case 1004
Set objXLBook = objXLApp.Workbooks.Open(conPath & "Generic.xlt")
Resume Next
Case 53
Resume Next
Case 75
Resume Next
Case Else
MsgBox Err.Description, vbExclamation, _
"Error " & Err.Number
End Select
End Sub
It is my first time writing code in VBA and am trying my best but this one has me stuck for about 4 hours now.
here is the code:
Sub exportspreadsheet()
On Error GoTo HandleError
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strSQL2 As String
Dim strFileName As String
Dim objXLApp As Object
Set objXLApp = CreateObject("Excel.Application")
Dim objXLBook As Excel.Workbook
conPath = "C:\Users\VasquezJr\Documents\"
On Error GoTo ExportReport_Error
strSQL = "Select Distinct Final_all.Store_ID From Final_all;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
rs.MoveLast
rs.MoveFirst
Do Until rs.EOF
' create a workbook from the template
Set objXLApp = New Excel.Application
Set objXLBook = objXLApp.Workbooks.Open(conPath & "MyTemplate.xltx")
objXLBook.SaveAs (conPath & "Store_" & rs!Store_ID & "_CPC_Report.xls")
objXLBook.Close
strSQL2 = " SELECT Final_all.Store_ID, Final_all.StorePC, Final_all.FSA, Final_all.[Delivery Mode], Final_all.[Abbreviated Name], Final_all.TOTAL, Final_all.Distance_km, Final_all.MaxOfRank, Final_all.Cumm_TOT " _
& "FROM Final_all " _
& "WHERE Final_all.Store_ID =" & rs!Store_ID _
& " ORDER BY Final_all.ID, Final_all.Store_ID, Final_all.StorePC;"
strFileName = [rs]![Store_ID]
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, strSQL2, strFileName, True
rs.MoveNext
strFileName = ""
strSQL2 = ""
Set tmpLocalTable = Nothing
Set tmpLocalQuery = Nothing
Loop
On Error GoTo 0
Exit Sub
ExportReport_Error:
MsgBox "Error " & Err.Number & "(" & Err.Description & ") in procedure ExportReports"
HandleError:
Select Case Err.Number
Case 3265
Resume Next
Case 1004
Set objXLBook = objXLApp.Workbooks.Open(conPath & "Generic.xlt")
Resume Next
Case 53
Resume Next
Case 75
Resume Next
Case Else
MsgBox Err.Description, vbExclamation, _
"Error " & Err.Number
End Select
End Sub