Hello,
I am using the following code to export 2 queries into a single Excel sheet. The code is working as intended until I rename the database. I had requested some help with testing and gave the database a new name so that it would not replace the master copy. At that point the code starting throwing errors stating that it could not find the Excel workbook. As soon as I changed the database name back it started working again.
Can anyone explain to me why a name change (to the db not the Excel document) would affect this or how to get around it? Also, what part of the following code would even be dependent on recognizing the database name?
Thanks
Private Sub AccAdj_Click()
Dim rstName As Recordset
Dim objApp As Object, objMyWorkbook As Object, objMySheet As Object, objMyRange As Object
DoCmd.SetWarnings False
DoCmd.OpenQuery "MedAdj_List_qry"
DoCmd.OpenQuery "DenAdj_List_qry"
DoCmd.OpenQuery "VisAdj_List_qry"
DoCmd.OpenQuery "OtherAdj_List_qry"
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "AcACt_qry", "Accrual_AdjustmentList.xlsx", True, "Accrual_Adjustments"
Set rstName = CurrentDb.OpenRecordset("AcSB_qry")
Set objApp = CreateObject("Excel.Application")
Set objMyWorkbook = objApp.Workbooks.Open("Accrual_AdjustmentList.xlsx ")
Set objMySheet = objMyWorkbook.Worksheets("Accrual_Adjustments")
Set objMyRange = objMySheet.Cells(objApp.ActiveSheet.UsedRange.Rows .Count + 2, 1)
With objMyRange
rstName.MoveFirst 'Rewind to the first record
.Clear
.CopyFromRecordset rstName
End With
objMyWorkbook.Save
objApp.Quit
Set ojbMyWorkbook = Nothing
Set objApp = Nothing
End Sub