Hi,
I am trying to make a VBA Solution that will at the click of a button send an email out to all of my customers with updates on there orders
I have got a code to send an email to each record however this means that if a customer has 40 orders they will receive 40 emails, where as I want to send them just one with all of the orders within it so my plan is to search through the records and find records with the same email and put these into a tempory table which I can use as the record set for my mailing sub however the current code I have seams to not function correctly or just crash my computer can anyone help?
please see code below
I am trying to make a VBA Solution that will at the click of a button send an email out to all of my customers with updates on there orders
I have got a code to send an email to each record however this means that if a customer has 40 orders they will receive 40 emails, where as I want to send them just one with all of the orders within it so my plan is to search through the records and find records with the same email and put these into a tempory table which I can use as the record set for my mailing sub however the current code I have seams to not function correctly or just crash my computer can anyone help?
please see code below
Code:
Option Compare Database
Private Sub Command2_Click()
Dim dbS As DAO.Database
Dim rst, drst As DAO.Recordset
Dim tblName, aVar As Variant
Dim sqlStr, Email, Order_ID, Name, Vart As String
Dim i, a As Integer
Set dbS = CurrentDb()
Set rst = dbS.OpenRecordset("Test")
Set drst = dbS.OpenRecordset("Test")
With rst
Do Until rst.EOF
Email = .Fields("Email_Address")
Name = .Fields("empName")
'loop through each record to find matches to first email
With drst
i = 0
Do Until drst.EOF
i = i + 1
drst.MoveFirst
If .Fields("Email_Address") = Email Then
Vart = Vart & I
Vart = .Fields("Order_ID")
End If
drst.MoveNext
Loop
End With
'open temp table and add record
dbS.OpenRecordset (Temp)
.Append
!Name = .Fields("Name")
!Email_Address = .Fields("Email_Address")
'loop through Order ids and add to new record
a = 0
Do Until Vart = ""
a = a + 1
Vart = Vart & a
Order_ID = Order_ID & a
!Order_ID = Vart
Loop
.Update
.Close
Loop
End With
End Sub
Last edited: