Eljefegeneo
Still trying to learn
- Local time
- Today, 14:53
- Joined
- Jan 10, 2011
- Messages
- 902
I am very new to loops, was afraid to try them but now one would come in very handy. I've copied come code that seems to work OK, but I cannot get the condition part of the report code to function correctly. What I am trying to do is loop through a query record set of ten items, print two different reports for each with the same condition.
The recordset is the query:
The code (modified so that I can check that the AgentID number has been passed to the condition) is:
When I run the code the AgentID is not passed to the "cond" which is
When I run the code I just get a message box saying:
"rptEmailReportCurrentMonth For Agent#: [AgentId]"
"rptEmailReportCurrentMonthReceived For Agent#: [AgentID]"
How do I pass the condition for each item in the loop? Thanks.
The recordset is the query:
Code:
SELECT tblAgents.CountryReference, tblAgents.AgentID, tblAgents.Active
FROM tblAgents
WHERE (((tblAgents.Active)=True));
Code:
On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim iCount As Integer
Dim rpt, rpt1, cond, cond1 As String
rpt = "rptEmailReportCurrentMonth"
rpt1 = "rptEmailReportCurrentMonthReceived"
cond = "[AgentID] = AgentID"
Set db = CurrentDb()
Set rs = db.OpenRecordset("qryAgentIdList")
cond = "[AgentID] = rs.AgentID"
With rs
'If .RecordCount <> 0 Then
rs.MoveFirst
iCount = rs.RecordCount
Do While Not .EOF
MsgBox rpt & " " & "For Agent#:" & " " & cond
MsgBox rpt1 & " " & "For Agent#:" & " " & cond
.MoveNext
Loop
'End If
End With
rs.Close
Error_Handler_Exit:
On Error Resume Next
Set rs = Nothing
Set db = Nothing
Exit Sub
Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
Err.Number & vbCrLf & "Error Source: LoopRecExample" & vbCrLf & "Error Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
Code:
cond = "[AgentID] = AgentID"
"rptEmailReportCurrentMonth For Agent#: [AgentId]"
"rptEmailReportCurrentMonthReceived For Agent#: [AgentID]"
How do I pass the condition for each item in the loop? Thanks.