Hi, me again!
I have the following code that pulls the logging users area, team, manager and email so that it can notify them of the error that has been logged.
It all works, which is good however it is sending the email twice!!
I have tested the SQL in Query Design and it only pulls one row of date (which is expected in this sql statement) however its sending the email twice.
Code as follows:
'Sub routine that sends an email to the team causing manager(s) when a breach is logged.
Public Sub SendBreachEmail()
On Error GoTo ErrSendBreachEmail:
Dim LoggedArea1 As String
Dim LoggedArea2 As String
Dim LoggedTeam1 As String
Dim LoggedTeam2 As String
LoggedArea1 = Form_frmLogBreach.cboAreaCaused1
LoggedArea2 = Nz(Form_frmLogBreach.cboAreaCaused2, "")
LoggedTeam1 = Form_frmLogBreach.cboTeamCaused1
LoggedTeam2 = Nz(Form_frmLogBreach.cboTeamCaused2, "")
Dim sql As String
Dim rec As dao.Recordset
'Assigns the right SELECT statement to sql depending on the number of team(s)/area(s) causing.
If IsNull(Form_frmLogBreach.cboAreaCaused2) Then
sql = "SELECT tblUsers.FullName, tblUsers.Email FROM tblUsers WHERE (((tblUsers.UserGrade)='Manager') AND ((tblUsers.Department)='" & LoggedArea1 & "') AND ((tblUsers.Team)='" & LoggedTeam1 & "'));"
Else
sql = "SELECT tblUsers.FullName, tblUsers.Email FROM tblUsers WHERE (((tblUsers.UserGrade)='Manager') AND ((tblUsers.Department)='" & LoggedArea1 & "') AND ((tblUsers.Team)='" & LoggedTeam1 & "')) OR (((tblUsers.Department)='" & LoggedArea2 & "') AND ((tblUsers.Team)='" & LoggedTeam2 & "'));"
End If
Set rec = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
'rec.MoveFirst
'Loops through each email address returned, sending an email to each.
Do Until rec.EOF
'MsgBox (rec!Email & " " & rec!FullName)
'Sends the actual email.
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = rec!Email
.From = "Error Reporting Tool"
.Subject = "AUTOMATED MESSAGE: A breach has been logged against your team."
.Body = "Dear " & rec!FullName & vbCrLf & vbCrLf & "An error has been logged against your team. Here is the summary:" & vbCrLf & vbCrLf & Form_frmLogBreach.txtTitle & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & "Error Reporting Tool"
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
rec.MoveNext
Loop
rec.Close
Set rec = Nothing
Exit Sub
ErrSendBreachEmail:
MsgBox ("An error occurred whilst sending the breach notification email: " & Err.Description)
mdlDatabaseActivity.ErrorID = Err.Number
mdlDatabaseActivity.ErrorDesc = Err.Description
mdlDatabaseActivity.CentralErrorHandler "mdlDatabaseActivity", "SendBreachEmail"
Resume Next
End Sub
Can anyone spot my mistake?
Thanks in advance.
Carl
I have the following code that pulls the logging users area, team, manager and email so that it can notify them of the error that has been logged.
It all works, which is good however it is sending the email twice!!
I have tested the SQL in Query Design and it only pulls one row of date (which is expected in this sql statement) however its sending the email twice.
Code as follows:
'Sub routine that sends an email to the team causing manager(s) when a breach is logged.
Public Sub SendBreachEmail()
On Error GoTo ErrSendBreachEmail:
Dim LoggedArea1 As String
Dim LoggedArea2 As String
Dim LoggedTeam1 As String
Dim LoggedTeam2 As String
LoggedArea1 = Form_frmLogBreach.cboAreaCaused1
LoggedArea2 = Nz(Form_frmLogBreach.cboAreaCaused2, "")
LoggedTeam1 = Form_frmLogBreach.cboTeamCaused1
LoggedTeam2 = Nz(Form_frmLogBreach.cboTeamCaused2, "")
Dim sql As String
Dim rec As dao.Recordset
'Assigns the right SELECT statement to sql depending on the number of team(s)/area(s) causing.
If IsNull(Form_frmLogBreach.cboAreaCaused2) Then
sql = "SELECT tblUsers.FullName, tblUsers.Email FROM tblUsers WHERE (((tblUsers.UserGrade)='Manager') AND ((tblUsers.Department)='" & LoggedArea1 & "') AND ((tblUsers.Team)='" & LoggedTeam1 & "'));"
Else
sql = "SELECT tblUsers.FullName, tblUsers.Email FROM tblUsers WHERE (((tblUsers.UserGrade)='Manager') AND ((tblUsers.Department)='" & LoggedArea1 & "') AND ((tblUsers.Team)='" & LoggedTeam1 & "')) OR (((tblUsers.Department)='" & LoggedArea2 & "') AND ((tblUsers.Team)='" & LoggedTeam2 & "'));"
End If
Set rec = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
'rec.MoveFirst
'Loops through each email address returned, sending an email to each.
Do Until rec.EOF
'MsgBox (rec!Email & " " & rec!FullName)
'Sends the actual email.
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = rec!Email
.From = "Error Reporting Tool"
.Subject = "AUTOMATED MESSAGE: A breach has been logged against your team."
.Body = "Dear " & rec!FullName & vbCrLf & vbCrLf & "An error has been logged against your team. Here is the summary:" & vbCrLf & vbCrLf & Form_frmLogBreach.txtTitle & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & "Error Reporting Tool"
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
rec.MoveNext
Loop
rec.Close
Set rec = Nothing
Exit Sub
ErrSendBreachEmail:
MsgBox ("An error occurred whilst sending the breach notification email: " & Err.Description)
mdlDatabaseActivity.ErrorID = Err.Number
mdlDatabaseActivity.ErrorDesc = Err.Description
mdlDatabaseActivity.CentralErrorHandler "mdlDatabaseActivity", "SendBreachEmail"
Resume Next
End Sub
Can anyone spot my mistake?
Thanks in advance.
Carl