alfonsolans
New Member
- Joined
- Jul 18, 2017
- Messages
- 5
Hello to all:
Team hope you are doing great today and thanks fin advance for your support in this great community, I have a huge question:
I am trying to create an excel document (template or not) with different worksheets that will be feed from different Access Queries, I started creating the module on VBA, it runs but instead of creating me only one file with all the different worksheets. it is creating different excel files with the information on every worksheet but in 6 different files, how can I close each query on my module to input the information on the same excel sheet instead on 6 of them, here is my module below:
Sub exportQueryADODB()
Dim dbs As Database
Set dbs = CurrentDb
Set rsQuery = dbs.OpenRecordset("In Transit Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("In Transit").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Last Night Arrival Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Last Night Arrival").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("On The Floor Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("On The Floor").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Assigned on a TR2 Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Assigned TR2").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Dispatched Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Dispatched").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Delivered Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Delivered").Range("A2").CopyFromRecordset rsQuery
End Sub
Team hope you are doing great today and thanks fin advance for your support in this great community, I have a huge question:
I am trying to create an excel document (template or not) with different worksheets that will be feed from different Access Queries, I started creating the module on VBA, it runs but instead of creating me only one file with all the different worksheets. it is creating different excel files with the information on every worksheet but in 6 different files, how can I close each query on my module to input the information on the same excel sheet instead on 6 of them, here is my module below:
Sub exportQueryADODB()
Dim dbs As Database
Set dbs = CurrentDb
Set rsQuery = dbs.OpenRecordset("In Transit Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("In Transit").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Last Night Arrival Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Last Night Arrival").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("On The Floor Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("On The Floor").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Assigned on a TR2 Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Assigned TR2").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Dispatched Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Dispatched").Range("A2").CopyFromRecordset rsQuery
Set rsQuery = dbs.OpenRecordset("Delivered Query Query")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("M:\Auto\Lean\Pallet Report\Pallet Report Complete.xlt")
targetWorkbook.Worksheets("Delivered").Range("A2").CopyFromRecordset rsQuery
End Sub