473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Find all records for a single customer and send them in e-mail

I have a table that contains customer records when they need an e-mail notification. The records only appear in the table when the customer needs a notification (this info is pulled from an HTML page). Sometimes there are multiple records for one customer because they need notification for multiple things. Right now I have a button that sends an email to the customer, but only for one record (its just a form that shows one record). What I want to do is compile all the records for a customer into one e-mail with the control of a button, rather than clicking through each record and sending individual e-mails. How can I do this?
Aug 7 '12 #1
6 4082
twinnyfo
3,653 Expert Mod 2GB
ryaanmichael,

First, how do you want to send the e-mails? I ask, because there are different ways to do this. If you want to send the data in the tables as an attached report, this will require different things. If you want to compile the body of an e-mail based on the multiple records, then this will require different coding. The principle will be the same though:

Build VBA code that builds a recordset based on the table. You may require two recordsets, once which is grouped by customer (so you only get the unique customer values) and one recordset with all the rest of the data associated with each customer.

Ther are multiple ways of doing this, either by filtering the data recordset by customer or by rebuilding a recordset each time a new customer record is reached.

Again, tell me your vision of what you want it to look like and we can move forward from there.....
Aug 7 '12 #2
Hi Twinnyfo,

My e-mail code send emails in Microsoft Outlook. The "send" button in my database pulls the data from a single record, puts it in the body of an outlook email, then "displays" it so I can send it out. It works great for one record, but I would love for the code to pull all records for a single user, and place all that data in the body of an e-mail.

Thanks for your help!
Aug 7 '12 #3
twinnyfo
3,653 Expert Mod 2GB
Perfect! Start with the code that you currently have and nset it within some other code.

Step one: You will need to create a recordset of all the unique customers that require e-mails (i.e you may have 27 records, but only 15 unique customers). You can do this several ways: 1) cretae a query that generates that list, then use that to create your recordset, or 2) just use the same SQL statement for the query in 1) above and use that in your code. I sometimes find it easier to create the query and then generate the recordset on the query.

Next, your code will loop through the unique customers one at a time. Using that unique customer, create a second recordset that only pulls that customer's records from the table. Count how many records that customer has, because you may want to tell your customer, "Hey, we found 3 orders from you!"

Generate the body of the e-mail, starting with the main header or introduciton--informaiton you will tell every customer. Then, cycle through the records for that customer, adding necessary information along the way. Once you get through all their records, add the footer or closing information (your contact info, etc).

Then generate the e-mail, bring it up for you to review before you send, just like you do now.

Once you click send, this is where your code should loop back to the beginning, find the next customer and do it all over again.

Believe it or not, this is really not a very difficult set of code to do, but I will assist you with any snags you may come across along the way.

If you have troubles, please post code or attach a sample copy of your db. I'd be glad to take a look and trouble shoot!

Best of luck!
Aug 7 '12 #4
Hi Twinnyfo,

Thanks for your reponse. Please forgive my ignorance, I'm not too experienced in VBA. How can I get my code to loop through the unique records and find all the records for the unique person?

I made a query that pull the the unique customer name, but how do I do the rest? Here is a simplified version of my current code that only pulls one record:

Expand|Select|Wrap|Line Numbers
  1. Private Sub notifyCustomer_Click()
  2.  
  3.  
  4. Dim strTo, strCC, strSubject, strBody As String
  5. Dim objOutlook
  6. Dim objEmail
  7. Dim customerName, notificationItem, notificationDate
  8.  
  9. Set customerName = Me.customerName
  10. Set noficationItem = Me.noficationItem 
  11. Set notificationDate = Me.notificationDate
  12.  
  13. strTo = customerName
  14. strCC = "Any other recipients"
  15. strSubject = "Email Subjet"
  16. strBody = customerName & notificationItem & notificationDate & " My email body"
  17.  
  18.  
  19. Set objOutlook = CreateObject("Outlook.application")
  20. Set objEmail = objOutlook.CreateItem(olMailItem)
  21.  
  22. With objEmail
  23.     .To = strTo
  24.     .CC = strCC
  25.     .Subject = strSubject
  26.     .HTMLBody = strBody
  27.     .Display
  28. End With
  29.  
  30.  
  31. Exit Sub
  32.  
  33. End Sub
Aug 7 '12 #5
twinnyfo
3,653 Expert Mod 2GB
First, I would include a command button for NotifyAll. Here is some code that should get you pretty close:

Expand|Select|Wrap|Line Numbers
  1. Private Sub NotifyAll_Click()
  2. On Error GoTo EH
  3.     Dim dbCustomers As Database
  4.     Dim rstCustomers As Recordset
  5.     Dim dbCustomerInfo As Database
  6.     Dim rstCustomerInfo As Recordset
  7.     Dim intCustomerRecords As Integer
  8.     Dim strSQL As String
  9.     Dim strTo As String
  10.     Dim strCC As String
  11.     Dim strSubject As String
  12.     Dim strBody As String
  13.     Set dbCustomers = CurrentDb()
  14.     strSQL = "SELECT * FROM tblCustomers GROUP BY CustomerName;"
  15.     Set rstCustomers = dbCustomers.OpenRecordset(strSQL, dbOpenDynaset)
  16.     If Not rstCustomers.EOF Then
  17.         rstCustomers.MoveFirst
  18.         Do While Not rstCustomers.EOF
  19.             strTo = rstCustomerInfo("CustomerEMail")
  20.             strCC = "Any other recipients"
  21.             strSubject = "E-mail Subject Line"
  22.             strBody = "Dear " & rstCustomers("CustomerName") & vbCrLf & vbCrLf
  23.  
  24.             strSQL = "SELECT * FROM tblCustomers WHERE CustomerName = '" & rstCustomers("CustomerName") & "';"
  25.             Set rstCustomerInfo = dbCustomers.OpenRecordset(strSQL, dbOpenDynaset)
  26.             If Not rstCustomerInfo.EOF Then
  27.                 rstCustomerInfo.MoveLast
  28.                 intCustomerRecords = rstCustomerInfo.RecordCount
  29.                 rstCustomerInfo.MoveFirst
  30.             Else
  31.                 intCustomerRecords = 0
  32.             End If
  33.             strBody = strBody & "We found " & intCustomerRecords & _
  34.                 " records on file with us." & vbCrLf & vbCrLf
  35.             Do While Not rstCustomerInfo.EOF
  36.                 strBody = strBody & rstCustomerInfo("NotificationItem") & _
  37.                     rstCustomerInfo("Notification Date") & vbCrLf
  38.             Loop
  39.             rstCustomerInfo.Close
  40.             strBody = strBody & "We appreciate your quick response to us, blah blah blah."
  41.             'NOTE:  for this type of e-mail, I prefer to use the SendObject Method...
  42.             DoCmd.SendObject acSendNoObject, , , strTo, strCC, , strSubject, strBody, True
  43.         Loop
  44.         dbCustomerInfo.Close
  45.         rstCustomers.Close
  46.         dbCustomers.Close
  47.     End If
  48.     Exit Sub
  49. EH:
  50.     MsgBox Err.Number & " " & Err.Description
  51.     Exit Sub
  52. Exit Sub
Aug 7 '12 #6
Hi Twinnyfo,

Thanks for your help again. I still can't get this to work. I updated the code your provided with the names of my querys/field names, but its still now working. I'm not sure I understand the strSQL line. My current query (qryUniqueCUSTOMER) provides the unique customer names already, should this SQL line be different? Here's what I got right now:

Expand|Select|Wrap|Line Numbers
  1. Private Sub NotifyAll_Click()
  2. On Error GoTo EH
  3.     Dim dbCUSTOMER As Database
  4.     Dim rstCUSTOMER As Recordset
  5.     Dim dbCUSTOMERInfo As Database
  6.     Dim rstCUSTOMERInfo As Recordset
  7.     Dim intCUSTOMERRecords As Integer
  8.     Dim CUSTOMER
  9.     Dim qryUniqueCUSTOMER
  10.     Dim itemNumber
  11.  
  12.     Dim strSQL, strTo, strCC, strSubject, strBody As String
  13.     Set itemNumber = Me.itemNumber
  14.     Set dbCUSTOMER = CurrentDb()
  15.     strSQL = "SELECT CUSTOMER FROM qryUniqueCUSTOMER GROUP BY CUSTOMER;"
  16.  
  17.     Set rstCUSTOMER = dbCUSTOMER.OpenRecordset(strSQL, dbOpenDynaset)
  18.  
  19.     If Not rstCUSTOMER.EOF Then
  20.         rstCUSTOMER.MoveFirst
  21.         Do While Not rstCUSTOMER.EOF
  22.             strTo = rstCUSTOMERInfo("CustomerEMail")
  23.             strCC = "CC Email addresses"
  24.             strSubject = "Notification for " & itemNumber
  25.             strBody = rstCUSTOMER("CUSTOMER") & vbCrLf & vbCrLf
  26.  
  27.             strSQL = "SELECT * FROM qryUniqueCUSTOMER WHERE CUSTOMER = '" & rstCUSTOMER("CUSTOMER") & "';"
  28.             Set rstCUSTOMERInfo = dbCUSTOMER.OpenRecordset(strSQL, dbOpenDynaset)
  29.             If Not rstCUSTOMERInfo.EOF Then
  30.                 rstCUSTOMERInfo.MoveLast
  31.                 intCUSTOMERRecords = rstCUSTOMERInfo.RecordCount
  32.                 rstCUSTOMERInfo.MoveFirst
  33.             Else
  34.                 intCUSTOMERRecords = 0
  35.             End If
  36.             strBody = strBody & "We found " & intCUSTOMERRecords & _
  37.                 " records on file with us." & vbCrLf & vbCrLf
  38.             Do While Not rstCUSTOMERInfo.EOF
  39.                 strBody = strBody & rstCUSTOMERInfo("NotificationItem") & _
  40.                     rstCUSTOMERInfo("Notification Date") & vbCrLf
  41.             Loop
  42.             rstCUSTOMERInfo.Close
  43.             strBody = strBody & "We appreciate your quick response to us, blah blah blah."
  44.             'NOTE:  for this type of e-mail, I prefer to use the SendObject Method...
  45.             DoCmd.SendObject acSendNoObject, , , strTo, strCC, , strSubject, strBody, True
  46.         Loop
  47.         dbCUSTOMERInfo.Close
  48.         rstCUSTOMER.Close
  49.         dbCUSTOMER.Close
  50.     End If
  51.     Exit Sub
  52. EH:
  53.     MsgBox Err.Number & " " & Err.Description
  54.     Exit Sub
  55. Exit Sub
  56.  
Aug 8 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim | last post by:
Hi, I have two tables (A and B) with id fields in them, I would like a list of ids that are in A but not in B. How would I do this in SQL? Thank you,
16
by: !TG | last post by:
I have a table with a date field. All I want to do it get all the records with a date before today. I tried the following: "SELECT * FROM StateLicenses Where (Exp < #07/26/2005#) Order By...
3
by: MostlyH2O | last post by:
Hi Folks, I have a query that joins 3 tables. One of the tables (SalaryData) has data where there may be duplicate records with different dates. Of those duplicate records, I want the query to...
2
by: Joost Kraaijeveld | last post by:
I have a table with column1, column2, column3 and column4. How do I get all records, sorted by column4 that have the same column1,column2 and column3? TIA Joost ...
1
by: fatesgrasp | last post by:
Hi. This is my first time trying this site out. I'm stuck on a problem. I'm trying to find records in an access database with a date greater than todays date but it's pulling all records. Please...
1
by: NEC | last post by:
I am using access 2000 and have a DAP that I created from a table. What I am trying to do is essentially create a find command that will search all records in the DAP based on user input. I have...
5
by: ciojr | last post by:
how do i write sql statment to find records with name greater than 17 characters.
0
by: mrsoft100 | last post by:
Dear Members I have written a program whose intention is to track fee payment by students in a school. the program is intended to track as many students as possible. My program works very well and...
21
by: DanicaDear | last post by:
I have a report named "rptHOTSTICKS_EXPIRING" based on a query named "HOSTICKS_SHIPPING_REPORT Query". The query contains these fields: ORDER_NUM (text) CUST_NUM (text) Name, address, contact...
5
by: Vikram Raman | last post by:
Hello , I have two tables that are Customer and Bill. Customer table has the following fields CustomerId,Name,Address,PhoneNo Bill table has the following fields
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.