Hello All!
I am hoping you can help me with a looping / querydef problem. I am trying to send an email to each supervisor who has an outstanding
follow-up on an audit based on the results from this query. I am using Access and Outlook 2016.
I have the following code, and it generates an email to each Supervisor, even if they do not have an outstanding audit because I can't
figure out how to pass any parameters - i.e.
to generate only the supervisors I need to email.
As well, this code, sends all of the outstanding audits to each supervisor, instead of the specific GroupID.
So I need to loop through each group ID and send an email that contains only the outstanding audits to each supervisor in that group.
Can anyone help me? :banghead:
I am hoping you can help me with a looping / querydef problem. I am trying to send an email to each supervisor who has an outstanding
follow-up on an audit based on the results from this query. I am using Access and Outlook 2016.
Code:
SELECT Audit.GroupID, Audit.TMName, Audit.Process, AuditCM.CMActivity, AuditCM.FUDate, AuditCM.FUComplete, SupvInfo.AreaID, SupvInfo.Email
FROM (Audit INNER JOIN SupvInfo ON Audit.GroupID = SupvInfo.GroupID);
figure out how to pass any parameters - i.e.
Code:
" WHERE qryAuditFollowUp.FUDate <=#" & Forms!frmSecurity!ShiftDate & "#" & " AND qryAuditFollowUp.FUComplete = False"
As well, this code, sends all of the outstanding audits to each supervisor, instead of the specific GroupID.
Code:
Public Sub SendFollowUpEmail()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim emailTo As String
Dim emailSubject As String
Dim emailText As String
Dim outApp As Outlook.Application
Dim outMail As Outlook.MailItem
Dim strSQL As String
Dim strBody As String
Dim rsBody As DAO.Recordset
Set db = CurrentDb()
'**creates an instance of Outlook
Set outApp = CreateObject("Outlook.application")
Set outMail = outApp.CreateItem(olMailItem)
Set rs = db.OpenRecordset("SELECT DISTINCT AreaName, Email" & _
" FROM qrySTWFollowUp")
Do Until rs.EOF
emailTo = rs.Fields("Email").Value
emailSubject = "Past Due Audit Follow-up(s)"
emailText = rs.Fields("AreaName").Value & " Audit Past Due:" & vbCrLf
strSQL = "SELECT * FROM qryAuditFollowUp " & _
" WHERE qryAuditFollowUp.FUDate <=#" & Forms!frmSecurity!ShiftDate & "#" & " AND qryAuditFollowUp.FUComplete = False"
Set rsBody = CurrentDb.OpenRecordset(strSQL)
If Not rsBody.RecordCount <> 0 Then
' do nothing
Else
'********************* Send outstanding audit results in a HTML table ********************
rsBody.MoveNext
Loop
rsBody.Close
End If
Set outMail = outApp.CreateItem(olMailItem)
outMail.To = emailTo
outMail.Subject = emailSubject
outMail.HTMLBody = emailText + strBody & "</Table>"
outMail.Display
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
So I need to loop through each group ID and send an email that contains only the outstanding audits to each supervisor in that group.
Can anyone help me? :banghead: