Can anyone tell me why this works when there is only one record in my supervisors table but doesn't when there is more than one record?
Sub SeparateEmails()
'*** error trapping - execution goes to bottom on error
'On Error GoTo Err_SeparateEmails
Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Dim rsGLTable As Recordset
Dim rsCriteria As DAO.Recordset
Dim rst As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("SELECT distinct [snum],[semail] FROM Supervisors")
'*** the first record in the Supervisors table ***
With rsCriteria
.MoveFirst
' With rs
' If .EOF Then
' Exit Sub
' Else
End With
'*** loop to move through the records in Supervisors table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Supervisors table
strSQL = "SELECT * FROM GLTable WHERE "
strSQL = strSQL & "[snum] = '" & rsCriteria![SNUM] & "'"
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "NewQuery"
Set qdf = db.CreateQueryDef("NewQuery", strSQL)
'
DoCmd.SendObject acReport, "rptGLTable", "RichTextFormat(*.rtf)", rsCriteria![semail], , , "Your Report", "Here is this week's report.", , ""
' *** goto the next record in Supervisors table
ContinueToNext:
rsCriteria.MoveNext
Loop
rsCriteria.Close
'
Exit_SeparateEmails:
Exit Sub
Err_SeparateEmails: '*** if there is an error, execution goes here
'*** if the error is the table or query missing (3265)
' then skip the delete line and resume on the next line
' Error 2501 notifies you that the SendObject action
' has been cancelled. See the OnNoData Event of the report.
If Err.Number = 3265 Or Err.Number = 2501 Then
Resume ContinueToNext
Else
'*** write out the error and exit the sub
MsgBox Err.Description
Resume Exit_SeparateEmails
End If
End Sub
Sub SeparateEmails()
'*** error trapping - execution goes to bottom on error
'On Error GoTo Err_SeparateEmails
Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Dim rsGLTable As Recordset
Dim rsCriteria As DAO.Recordset
Dim rst As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("SELECT distinct [snum],[semail] FROM Supervisors")
'*** the first record in the Supervisors table ***
With rsCriteria
.MoveFirst
' With rs
' If .EOF Then
' Exit Sub
' Else
End With
'*** loop to move through the records in Supervisors table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Supervisors table
strSQL = "SELECT * FROM GLTable WHERE "
strSQL = strSQL & "[snum] = '" & rsCriteria![SNUM] & "'"
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "NewQuery"
Set qdf = db.CreateQueryDef("NewQuery", strSQL)
'
DoCmd.SendObject acReport, "rptGLTable", "RichTextFormat(*.rtf)", rsCriteria![semail], , , "Your Report", "Here is this week's report.", , ""
' *** goto the next record in Supervisors table
ContinueToNext:
rsCriteria.MoveNext
Loop
rsCriteria.Close
'
Exit_SeparateEmails:
Exit Sub
Err_SeparateEmails: '*** if there is an error, execution goes here
'*** if the error is the table or query missing (3265)
' then skip the delete line and resume on the next line
' Error 2501 notifies you that the SendObject action
' has been cancelled. See the OnNoData Event of the report.
If Err.Number = 3265 Or Err.Number = 2501 Then
Resume ContinueToNext
Else
'*** write out the error and exit the sub
MsgBox Err.Description
Resume Exit_SeparateEmails
End If
End Sub