uphantom88
New member
- Local time
- Today, 08:23
- Joined
- Aug 28, 2020
- Messages
- 4
Hi pro.,
I'm working on a access database to manage training course.. I would like to brief the situation as following:
- When open form tracking associate training status , It will automatically send email to [SupervisorEmail]
- To illustrate for my case: we have table [qryEmailSupData] as below
When I open form/report which show above table, it will send emails to Ass015, Ass011, Ass001 . Example email to Ass015:
Hi Ass015, Please refer below table for your subordinate training status
Thanks & Best regards
As I'm not good at VBA, I intend to approach like this:
Please kindly advise as I encounter "Run-time error '3265' Item not found in this collection" and vba indicate problem at line which I defined strQryEmailBody
I'm working on a access database to manage training course.. I would like to brief the situation as following:
- When open form tracking associate training status , It will automatically send email to [SupervisorEmail]
- Access will loop through [SupervisorEmail] and find unique value of supervisor email
- For each Supervisor email, access will send email with the rows where that email is present (also including the header)
IDassociate | AssociateName | CourseName | Status | ExpiredDate | DirectSupID | SupervisorEmail |
Ass001 | Felix | General information | Overdue | 02-Dec-20 | Ass015 | Ass015 @ gmail.com |
Ass001 | Felix | Advance Access | Not yet attended | Ass015 | Ass015 @ gmail.com | |
Ass002 | Johnny | General information | Due within 1 month | 21-Dec-20 | Ass011 | Ass011 @ gmail.com |
Ass001 | Felix | Basic Python | Not yet attended | Ass015 | Ass015 @ gmail.com | |
Ass003 | Tom | General information | Overdue | 02-Dec-20 | Ass001 | Ass001 @ gmail.com |
Ass002 | Johnny | Basic Python | Due within 1 month | 25-Dec-20 | Ass015 | Ass015 @ gmail.com |
When I open form/report which show above table, it will send emails to Ass015, Ass011, Ass001 . Example email to Ass015:
Hi Ass015, Please refer below table for your subordinate training status
IDassociate | AssociateName | CourseName | Status | ExipredDate | DirectSupID | SupervisorEmail |
Ass001 | Felix | General information | Overdue | 02-Dec-20 | Ass015 | Ass015 @ gmail.com |
Ass001 | Felix | Advance Access | Not yet attended | Ass015 | Ass015 @ gmail.com | |
Ass001 | Felix | Basic Python | Not yet attended | Ass015 | Ass015 @ gmail.com | |
Ass002 | Johnny | Basic Python | Due within 1 month | 25-Dec-20 | Ass015 | Ass015 @ gmail.com |
Thanks & Best regards
As I'm not good at VBA, I intend to approach like this:
- Query Unique [SupervisorEmail] from table [qryEmailSupData] and name the query as [QryEmailSup]
- Loop through Recordset in [QryEmailSup]
- Loop through Recordset in [qryEmailSupData] with the "WHERE" condition is the current Record of the [qryEmailSup]
- Send email with the content of the filtered table
- I did looking around and combine some code on the internet but not successful, my copied & modified code is as following
Rich (BB code):
Public Sub SendSerialEmail()
Dim olApp As Object
Dim olItem As Variant
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQryEmailBody As String
Dim aHead(1 To 7) As String
Dim aRow(1 To 7) As String
Dim aBody() As String
Dim lCnt As Long
'Create the header row
aHead(1) = "ID Associate"
aHead(2) = "Name"
aHead(3) = "Course Name"
aHead(4) = "Status"
aHead(5) = "Expired Date"
aHead(6) = "DirectSupID"
aHead(7) = "SupervisorEmail"
lCnt = 1
ReDim aBody(1 To lCnt)
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>"
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT DirectSupID, AssociateName, Email FROM qryEmailSup")
Do Until rs.EOF
'Create each body row
strQryEmailBody = "SELECT *, qryEmailSupData.DirectSupID" & vbCrLf & _
"FROM qryEmailSupData " & vbCrLf & _
"WHERE (((qryEmailSupData.DirectSupID)=" & rs.Fields("DirecSupID").Value & "));"
Set rec = db.OpenRecordset(strQryEmailBody)
If Not (rec.BOF And rec.EOF) Then
Do While Not rec.EOF
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("ID Associate")
aRow(2) = rec("Name")
aRow(3) = rec("Course Name")
aRow(4) = rec("Status")
aRow(5) = rec("Expired Date")
aRow(6) = rec("DirectSupID")
aRow(7) = rec("SupervisorEmail")
aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
rec.MoveNext
Loop
End If
aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
'create the email
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(0)
olItem.display
olItem.To = rs.Fields("Email").Value
olItem.Subject = "Test E-mail"
olItem.htmlbody = Join(aBody, vbNewLine)
olItem.display
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Please kindly advise as I encounter "Run-time error '3265' Item not found in this collection" and vba indicate problem at line which I defined strQryEmailBody