General explanation:
I designate static values to each module (row, column and sheet name in excel).
For instance, 2_Total query is exported to row 2, column 3 of Total sheet.
6_sales_A is exported to row 3, column 3 of Total sheet.
Ph_p is exported to row 12, column 2 of Ph sheet.
Problem
It is extremely tedious for me to create & run too many modules. (124 queries, 124 modules)
Can anyone please help me find the most effective way to accomplish my task?
VBA
Option Compare Database
Option Explicit
Public Function Trans2()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim acRng As Variant
Dim xlRow As Integer
Dim db As DAO.Database
Dim qry As QueryDef
Dim rst As Recordset
Dim prm As DAO.Parameter
Dim strSQL As String
Set db = CurrentDb
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\J\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Wafer Cust")
xlRow = (xlWS.Columns("B").End(xlDown).Row)
Set qry = db.QueryDefs("6_WH sales_Por") //Query Name
qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate]
Set rst = qry.OpenRecordset(dbOpenDynaset)
Dim c As Integer
c = 2 'Assign Column number
xlRow = xlRow + 1 'Assign Row number
Do Until rst.EOF
For Each acRng In rst.Fields
xlWS.Cells(xlRow, c).Formula = acRng
c = c + 1
Next acRng
xlRow = xlRow + 1
c = 1
rst.MoveNext
If xlRow > 25 Then GoTo rq_Exit
Loop
rq_Exit:
rst.Close
Set rst = Nothing
Set xlWS = Nothing
xlWB.Close acSaveYes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Function
End Function
SQL
PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]));
Please let me know if you need any clarification that I can address. I will be responsive. Thank you so much for you help!
- Users input start date and end date on a form that filters my query 2_Total (single value) e.g. 154,21 (one of my query image attached)
- Run the VBA function that exports the query to an excel file
I designate static values to each module (row, column and sheet name in excel).
For instance, 2_Total query is exported to row 2, column 3 of Total sheet.
6_sales_A is exported to row 3, column 3 of Total sheet.
Ph_p is exported to row 12, column 2 of Ph sheet.
Problem
It is extremely tedious for me to create & run too many modules. (124 queries, 124 modules)
Can anyone please help me find the most effective way to accomplish my task?
VBA
Option Compare Database
Option Explicit
Public Function Trans2()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim acRng As Variant
Dim xlRow As Integer
Dim db As DAO.Database
Dim qry As QueryDef
Dim rst As Recordset
Dim prm As DAO.Parameter
Dim strSQL As String
Set db = CurrentDb
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\J\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Wafer Cust")
xlRow = (xlWS.Columns("B").End(xlDown).Row)
Set qry = db.QueryDefs("6_WH sales_Por") //Query Name
qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate]
Set rst = qry.OpenRecordset(dbOpenDynaset)
Dim c As Integer
c = 2 'Assign Column number
xlRow = xlRow + 1 'Assign Row number
Do Until rst.EOF
For Each acRng In rst.Fields
xlWS.Cells(xlRow, c).Formula = acRng
c = c + 1
Next acRng
xlRow = xlRow + 1
c = 1
rst.MoveNext
If xlRow > 25 Then GoTo rq_Exit
Loop
rq_Exit:
rst.Close
Set rst = Nothing
Set xlWS = Nothing
xlWB.Close acSaveYes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Function
End Function
SQL
PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]));
Please let me know if you need any clarification that I can address. I will be responsive. Thank you so much for you help!