Question : Access Loop Query

Hi

I have a form that accepts 3 inputs  last month, current month, id

I want to put a button on the form that does the following:

Create a table (or record set) listing all the id_1 values from a table (ACCT_TABLE)

Run a query (Qry(c)) that exports a separate spreadsheet for each entry in this table/recordset.  This query is based on two other queries (Qry(a) and Qry(b)) that accept the form inputs.

I had no idea how to approach this so went digging on the web.  The code below is a reworking of some I found online but it doesnt work and I dont understand it enough to know why.

Could anyone help or suggest a different way to get this done?

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
Private Sub Command11_Click()
 
Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim qdfCurr As DAO.QueryDef
Dim strFolder As String
Dim strSQL As String
 
strFolder = "T:\Access Testing"
 
Set dbCurr = CurrentDb()
 
Set rsCurr = dbCurr.OpenRecordset("SELECT ACCT_TABLE.ID_1 FROM ACCT_TABLE WHERE (((ACCT_TABLE.ID_18) = [Forms]![Form2]![Text6])) GROUP BY ACCT_TABLE.ID_1")
 
Set qdfCurr = dbCurr.CreateQueryDef("qryTemp")
 
Do While rsCurr.EOF = False
 
qdfCurr.SQL = SELECT [Qry(a)].ID_1, [Qry(a)].ID_10, [Qry(a)].ID_11, [Qry(a)].ACCT_ID, [Qry(a)].ID_8, [Qry(a)].ID_22, [Qry(a)].ID_5, [Qry(a)].SumOfVOLUMES AS Nov, [Qry(b)].SumOfVOLUMES AS [Dec], [Qry(a)]!SumOfVOLUMES-[Qry(b)]!SumOfVOLUMES AS Movement FROM [Qry(a)] INNER JOIN [Qry(b)] ON ([Qry(a)].ACCT_ID = [Qry(b)].ACCT_ID) AND ([Qry(a)].ID_1 = [Qry(b)].ID_1) GROUP BY [Qry(a)].ID_1, [Qry(a)].ID_10, [Qry(a)].ID_11, [Qry(a)].ACCT_ID, [Qry(a)].ID_8, [Qry(a)].ID_22, [Qry(a)].ID_5, [Qry(a)].SumOfVOLUMES, [Qry(b)].SumOfVOLUMES, [Qry_Large Movements(a)]!SumOfVOLUMES-[Qry(b)]!SumOfVOLUMES WHERE Qry(a)].ID_1 = '" & rsCurr! ACCT_TABLE.ID_1 & "'
 
qdfCurr.Close
 
DoCmd.TransferSpreadsheet acExport, , "qryTemp", strFolder & "Details for " & rsCurr!ACCT_TABLE.ID_1 & ".xls", True
 
rsCurr.MoveNext
 
Loop
 
rsCurr.Close
 
Set rsCurr = Nothing
Set qdfCurr = Nothing
Set dbCurr = Nothing
 
End Sub

Answer : Access Loop Query

Build a query in your db named qryTemp ... base it on any table, doesn't really matter. Change your code as below.

Is Qry(a).ID_1 a Text field? If not, change the last line of the .SQL to this:

& " [Qry_Large Movements(a)]!SumOfVOLUMES-[Qry(b)]!SumOfVOLUMES WHERE Qry(a)].ID_1 = " & rsCurr! ACCT_TABLE.ID_1

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim qdfCurr As DAO.QueryDef
Dim strFolder As String
Dim strSQL As String
 
strFolder = "T:\Access Testing"
 
Set dbCurr = CurrentDb()
Set rsCurr = dbCurr.OpenRecordset("SELECT ACCT_TABLE.ID_1 FROM ACCT_TABLE WHERE (((ACCT_TABLE.ID_18) = [Forms]![Form2]![Text6])) GROUP BY ACCT_TABLE.ID_1")
 
Do While rsCurr.EOF = False
 Set qdfCurr = dbCurr.QueryDefs("qryTemp")
 
qdfCurr.SQL = "SELECT [Qry(a)].ID_1, [Qry(a)].ID_10, [Qry(a)].ID_11, [Qry(a)].ACCT_ID, [Qry(a)].ID_8, [Qry(a)].ID_22, [Qry(a)].ID_5," _
 & "[Qry(a)].SumOfVOLUMES AS Nov, [Qry(b)].SumOfVOLUMES AS [Dec], [Qry(a)]!SumOfVOLUMES-[Qry(b)]!SumOfVOLUMES AS Movement FROM [Qry(a)] INNER JOIN" _
 & "[Qry(b)] ON ([Qry(a)].ACCT_ID = [Qry(b)].ACCT_ID) AND ([Qry(a)].ID_1 = [Qry(b)].ID_1) GROUP BY [Qry(a)].ID_1, [Qry(a)].ID_10, [Qry(a)].ID_11," _
 & " [Qry(a)].ACCT_ID, [Qry(a)].ID_8, [Qry(a)].ID_22, [Qry(a)].ID_5, [Qry(a)].SumOfVOLUMES, [Qry(b)].SumOfVOLUMES," _
 & " [Qry_Large Movements(a)]!SumOfVOLUMES-[Qry(b)]!SumOfVOLUMES WHERE Qry(a)].ID_1 = '" & rsCurr! ACCT_TABLE.ID_1 & "'
 
qdfCurr.Close
 
DoCmd.TransferSpreadsheet acExport, , "qryTemp", strFolder & "Details for " & rsCurr!ACCT_TABLE.ID_1 & ".xls", True
 
rsCurr.MoveNext
 
Loop
 
rsCurr.Close
Random Solutions  
 
programming4us programming4us