Hi,
Help needed.
I Got these modules, to automate emails, from a sample database and I'm trying to alter them to fit my database. I have them working but they are both called the same name "SeparateEmails()" so when I call the sub I get an "ambiguous name detected" also as I had to convert the sample database from access 97 to 2000 I was wondering if the code needed to be updated a little too. Also could someone explain to me why there is 2 of them, the first one seems to create the query and then send out a million emails to the same person but the second one will not start if the query does not exist and there's an annoying message box from outlook that I would need to say yes to for every email.. %-)
Module1:
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 rstblAutoEmail As Recordset
Dim rsCriteria As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("tblAutoEmail", dbOpenSnapshot)
'*** the first record in the Criteria table ***
rsCriteria.MoveFirst
'*** loop to move through the records in Criteria table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Criteria table
strSQL = "SELECT * FROM tblAutoEmail WHERE "
strSQL = strSQL & "[PPlayerId] = " & rsCriteria![PPlayerId] & ""
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "QryAutoEmailReport"
Set qdf = db.CreateQueryDef("QryAutoEmailReport", strSQL)
DoCmd.SendObject acReport, "RptAutoEmailReport", "RichTextFormat(*.rtf)", rsCriteria![PPlayer], "", "", "This is a test", "I am testing a new idea for reports", False, ""
rsCriteria![Emailed] = True
'*** goto the next record in Criteria table
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 Next
Else
'*** write out the error and exit the sub
MsgBox Err.Description
Resume Exit_SeparateEmails
End If
End Sub
Module2
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 rstblAutoEmail As Recordset
Dim rsCriteria As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("tblPlayer2", dbOpenDynaset)
'*** the first record in the Criteria table ***
rsCriteria.MoveFirst
'*** loop to move through the records in Criteria table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Criteria table
strSQL = "SELECT * FROM tblAutoEmail WHERE "
strSQL = strSQL & "[PPlayerId] = " & rsCriteria![PPlayerId] & ""
'strSQL = strSQL & " and [QPoints] = " & rsCriteria![PPlayerId2] & ""
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "QryAutoEmailReport"
Set qdf = db.CreateQueryDef("QryAutoEmailReport", strSQL)
DoCmd.SendObject acReport, "RptAutoEmailReport", "RichTextFormat(*.rtf)", rsCriteria![PPlayer], "", "", "This is a test", "I am testing a new idea for reports", False, ""
rsCriteria.Edit
rsCriteria![Emailed] = True
rsCriteria.Update
'*** goto the next record in Criteria 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
Help needed.
I Got these modules, to automate emails, from a sample database and I'm trying to alter them to fit my database. I have them working but they are both called the same name "SeparateEmails()" so when I call the sub I get an "ambiguous name detected" also as I had to convert the sample database from access 97 to 2000 I was wondering if the code needed to be updated a little too. Also could someone explain to me why there is 2 of them, the first one seems to create the query and then send out a million emails to the same person but the second one will not start if the query does not exist and there's an annoying message box from outlook that I would need to say yes to for every email.. %-)
Module1:
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 rstblAutoEmail As Recordset
Dim rsCriteria As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("tblAutoEmail", dbOpenSnapshot)
'*** the first record in the Criteria table ***
rsCriteria.MoveFirst
'*** loop to move through the records in Criteria table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Criteria table
strSQL = "SELECT * FROM tblAutoEmail WHERE "
strSQL = strSQL & "[PPlayerId] = " & rsCriteria![PPlayerId] & ""
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "QryAutoEmailReport"
Set qdf = db.CreateQueryDef("QryAutoEmailReport", strSQL)
DoCmd.SendObject acReport, "RptAutoEmailReport", "RichTextFormat(*.rtf)", rsCriteria![PPlayer], "", "", "This is a test", "I am testing a new idea for reports", False, ""
rsCriteria![Emailed] = True
'*** goto the next record in Criteria table
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 Next
Else
'*** write out the error and exit the sub
MsgBox Err.Description
Resume Exit_SeparateEmails
End If
End Sub
Module2
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 rstblAutoEmail As Recordset
Dim rsCriteria As Recordset
Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("tblPlayer2", dbOpenDynaset)
'*** the first record in the Criteria table ***
rsCriteria.MoveFirst
'*** loop to move through the records in Criteria table
Do Until rsCriteria.EOF
'*** create the Select query based on
' the first record in the Criteria table
strSQL = "SELECT * FROM tblAutoEmail WHERE "
strSQL = strSQL & "[PPlayerId] = " & rsCriteria![PPlayerId] & ""
'strSQL = strSQL & " and [QPoints] = " & rsCriteria![PPlayerId2] & ""
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "QryAutoEmailReport"
Set qdf = db.CreateQueryDef("QryAutoEmailReport", strSQL)
DoCmd.SendObject acReport, "RptAutoEmailReport", "RichTextFormat(*.rtf)", rsCriteria![PPlayer], "", "", "This is a test", "I am testing a new idea for reports", False, ""
rsCriteria.Edit
rsCriteria![Emailed] = True
rsCriteria.Update
'*** goto the next record in Criteria 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