473,569 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to send emails automatically from an access database query?

1 New Member
I have a MS Access 2010 db taht I want to send emails from automatically. I have the query setup but am getting stuck with the CDO VBA. They query is call 'qryEmails' and contains the following 4 fields:

ReturnCode\Sale sOrderNumber\Na me\EmailAddress

How do I get access to: 1. Loop through each record and send an email to each email address listed 2. In each email, have a message that will contain reference to the first 3 fields, so each message appears personalised 3. Have a dynamic subject, so the ReturnCode field is in each subject

I have been trying small steps at first, so far I am receiving 100's of emails to the same address. Here is my code (I have used XXX where I do not want to disclose info):

Expand|Select|Wrap|Line Numbers
  1. Dim rst As ADODB.Recordset 
  2. Dim strSQL As String 
  3. Dim strEmail As String 
  4. Set rst = New ADODB.Recordset 
  5. ' strSQL = "[qryEmails]" 
  6. 'source of recordset rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic 
  7. ' Do While Not rst.EOF strEmail = rst.Fields("EmailAddress")
  8.  
  9. Set objMessage = CreateObject("CDO.Message")
  10. objMessage.Subject = "Your refund is:" '
  11. objMessage.FROM = """SENDER"" <XXX@somewhere.com>"
  12. objMessage.To = rst.Fields("EmailAddress")
  13. objMessage.TextBody = objMessage.TextBody & rst(1)
  14.  
  15.  
  16. '==Add fields to email body
  17. 'Do While strEmail = rst.Fields("EmailAddress")
  18.  
  19. 'rst.MoveNext
  20. 'If rst.EOF Then Exit Do
  21. 'Loop
  22. ' ========= SMTP server configuration
  23.  
  24.     objMessage.Configuration.Fields.Item _
  25.      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  26.  
  27.     'Name or IP of Remote SMTP Server
  28.     objMessage.Configuration.Fields.Item _
  29.     ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "XXX"
  30.  
  31.     'Server port (typically 25)
  32.     objMessage.Configuration.Fields.Item _
  33.     ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  34.  
  35.     objMessage.Configuration.Fields.Update
  36.  
  37.     '==End remote SMTP server configuration section==
  38.  
  39.     'Send email
  40.     objMessage.Send
  41.     'Clear variable for next loop
  42.     Set objMessage = Nothing
  43. Loop
  44. rst.Close Set rst = Nothing
Any ideas why this is sending 100's of emails? The query results so far is only returning two addresses for testing purposes.

Regards,

Michael
Dec 16 '13 #1
1 4765
zmbd
5,501 Recognized Expert Moderator Expert
How do I get access to: 1. Loop through each record and send an email to each email address listed 2. In each email, have a message that will contain reference to the first 3 fields, so each message appears personalised 3. Have a dynamic subject, so the ReturnCode field is in each subject
You have three questions. We ask that there be only one question per thread. I makes it easier to keep on topic and for other members to find the information they are looking when searching.

Please choose ONE and clearly describe what it is that you are having issues with in completing that goal. The more detail you provide the easier it is for us to help you.



Oh, and your posted code is not usable as given. Please post the actual working code

As for the commented out code
Expand|Select|Wrap|Line Numbers
  1.  
  2. '==Add fields to email body
  3. 'Do While 
  4. 'strEmail = rst.Fields("EmailAddress")
  5. 'rst.MoveNext
  6. 'If rst.EOF Then Exit Do
  7. 'Loop
If this were to actually run with a valid recordset, fix the syntax, then every record with a value in [EmailAddress] would be added to the strEmail string.

You're also mising the "DO" for the "Loop" on line 43
Dec 17 '13 #2

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

Similar topics

5
3783
by: powerrun | last post by:
Help! I have tons of info loaded into an excel spreadsheet that I need to transfer into an existing database in access. There is a specific Query I need the information to transfer to. This Query has existing records in it that I don't want to over-write. Is this possible?? Melissa
1
4311
by: Bj?rn Keil | last post by:
Hi, first of all I have to say I'am new to MS Access, I'm used to database systems like Oracle or MySQL. I created a simple database consisting of a single table and a form to fill in new entrys and search for existing entrys. But the search function that Access generates as I created the proper button is way to simple, I need a function...
1
4293
by: gpaille | last post by:
I got a big Access database I'd like to convert to MS SQL Server 2000 and I'm looking for an efficient automated tool to convert data (over 1 400 tables), query, forms and interface. Does somebody can provide me with a list or, at least,a suggestion of tools that has already been used or well known for their efficiency.(except upsizing...
1
1578
by: nilsonj | last post by:
Trying to set up an admin page for an online training registration page. I am having trouble with a sql statement. Here is the info on the tables. I have two tables: classPerson and reglist classPerson - has two fields: idr, idc *idr = id number for a person idc = id number for a class being offered that they have signed up for
23
2759
by: Bosnoval | last post by:
Access 2003 Databse ASP.NET 2 Win 2003 Server Currently I have a search page that only allows a single keyword (person) to be searched at a time. I thought upgrading that option via a multiple selection listbox would be better so that you can search for a pair or more of people at a time but the query that makes this happen is giving me a...
3
2955
by: drnorbert | last post by:
What is the best way to receive and send emails in MsAccess? Could this be done without Outlook automatation?
5
2588
by: harky | last post by:
Hi, I am looking for help with a project. I am looking for a database to show me who has not payed thier bill after 30 days and if this could be a pop message. I need it to be on a webpage then i need it to send an email to the client who has not paid asking to settle the bill Thanks Claire
1
1846
by: Eglute | last post by:
Hello I want to show the results of MS Access database query in Visual basic. Do you know how to do this? I want to show the results in objects such as label or text box. Please help me. My code is: Dim db As Database Dim rec As Recordset Private Sub Command1_Click() Set db = OpenDatabase("the patht of database") MsgBox "database is...
1
1161
by: ng250009 | last post by:
I am using access database .i want to populate a query which includes i mean which call a function in the query.Finally i want the exact query which populates the variables and aswellas the function calling query.for example i want to display empname,empcode,transactiontime,transactiondate and i want to include one function which changes the...
1
2075
by: hrprabhu | last post by:
Hi All, How do you Send SMS from Access database? I received a SMS from my dentist and the receptionist told me that the database sent it. I was wondering how it is done. Raghu Prabhu
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.