IgnoranceIsBliss
Registered User.
- Local time
- Today, 15:13
- Joined
- Jun 13, 2019
- Messages
- 35
Hi all - I have a Customers table that has roughly 4,000 rows in it (and growing). I am attempting to automate some things that are currently manually done.
Below is the process..
1) Query Table1 For all customers whose ContactFUTimeFrame is current month
2) Create Two groups of the current month customers
a. Group 1 = New Sale
b. Group 2 = Follow-Up
3) Capture the Contact Name, Contact Email and Folder Location for the Customer
4) Create an email Draft
I have put together this syntax, which debugs perfect, but when I attempt to press the button and hopefully have Drafts created, I get no draft created and no errors displayed.
Can someone help me here with what is going wrong with my code?
Below is the process..
1) Query Table1 For all customers whose ContactFUTimeFrame is current month
2) Create Two groups of the current month customers
a. Group 1 = New Sale
b. Group 2 = Follow-Up
3) Capture the Contact Name, Contact Email and Folder Location for the Customer
4) Create an email Draft
I have put together this syntax, which debugs perfect, but when I attempt to press the button and hopefully have Drafts created, I get no draft created and no errors displayed.
Can someone help me here with what is going wrong with my code?
Code:
Option Compare Database
Private Sub Command2_Click()
Dim currMonth As String
Dim contact As String
Dim contactEmail As String
Dim customerFolder As String
Dim emailBody As String
Dim emailSubject As String
Dim rsNS As DAO.Recordset
Dim rsFU As DAO.Recordset
Dim nsAll As Variant
Dim fuAll As Variant
Dim ns As Variant
Dim fu As Variant
'Get current month
currMonth = MonthName(Month(Now), True)
'Create array to house New Sale for current month
Set rsNS = CurrentDb.OpenRecordset("Select ClientName FROM Table1 WHERE Mid(ContactFUTimeFrame, InStrRev(ContactFUTimeFrame, ' ') + 1) = " & currMonth & " And ContactType = 'New Sale'")
rsNS.MoveFirst
rsNS.MoveLast
nsAll = rsNS.GetRows(rsNS.RecordCount)
'Create Array to house Follow Up for current month
Set rsFU = CurrentDb.OpenRecordset("Select ClientName FROM Table1 WHERE Mid(ContactFUTimeFrame, InStrRev(ContactFUTimeFrame, ' ') + 1) = " & currMonth & " And ContactType = 'Follow-Up'")
rsFU.MoveFirst
rsFU.MoveLast
fuAll = rsFU.GetRows(rsFU.RecordCount)
'Iterate the array
For Each ns In nsAll
'Grab the contact Name
contact = DLookup("ClientContactName", "Table1", "ClientName = '" & CStr(ns) & "'")
'Grabbing Customer Folder
customerFolder = DLookup("LocalCustomerFolder", "Table1", "ClientName = '" & CStr(ns) & "'")
'Grabbing Customer Email
contactEmail = DLookup("ClientContactEmail", "Table1", "ClientName = '" & CStr(ns) & "'")
'Check if Customer Name field has one name or multiple
ValidateInput (contact)
'If Not All take first name only
If contact <> "All" Then
contact = Left(contact, InStr(1, contact, " ") - 1)
End If
'Set email info
emailBody = "<p>Hi " & contact & ",</p><p>Test test test test test test test test test test test test test. Thanks!</p>"
emailSubject = Year(Date) & " New Sale Information - " & CStr(ns)
'Creating the email draft
CreateEmail emailSubject, emailBody, customerFolder, contactEmail
Next
'Iterate the array
For Each fu In fuAll
'Grab the contact Name
contact = DLookup("ClientContactName", "Table1", "ClientName = '" & CStr(ns) & "'")
'Grabbing Customer Folder
customerFolder = DLookup("LocalCustomerFolder", "Table1", "ClientName = '" & CStr(ns) & "'")
'Grabbing Customer Email
contactEmail = DLookup("ClientContactEmail", "Table1", "ClientName = '" & CStr(ns) & "'")
'Check if Customer Name field has one name or multiple
ValidateInput (contact)
'If Not All take first name only
If contact <> "All" Then
contact = Left(contact, InStr(1, contact, " ") - 1)
End If
'Set email info
emailBody = "<p>Hi " & contact & ",</p><p>Test test test test test test test test test test test test test. Thanks!</p>"
emailSubject = Year(Date) & " Follow-Up Email - " & CStr(ns)
'Creating the email draft
CreateEmail emailSubject, emailBody, customerFolder, contactEmail
Next
End Sub
Private Function CreateEmail(emailSubject As String, emailBody As String, customerFolder As String, contactEmail As String)
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatHTML
.To = contactEmail
.Subject = emailSubject
.HTMLBody = emailBody
.Save
End With
End Function
Private Function ValidateInput(cn As String)
If InStr(cn, ",") = 0 Then
contactName = cn
Else
contactName = "All"
End If
End Function