Results 1 to 4 of 4
  1. #1
    nablmm is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    2

    Send email to multiple recipients based on query


    Hello,

    I am running access 2010 and would like to have a macro that runs a query and emails individuals through outlook (exchange) based on the results of that query. Specifically, if the query yields no results, no one should be emailed. One of the columns in the query is the individuals email address, therefore for each result in the query, I would like an email to be sent to that individual. However, if there are multiple results for the same individual, I would like those results to be sent in the same email as opposed to one email per result.

    Thank you in advance for your help!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,953
    Not a macro, use VBA. Common topic in forum. Here is recent one for starters: https://www.accessforums.net/macros/...rts-46472.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    nablmm is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    2
    Thanks for the link June7. I saw the basic structure for the code (below) as well as your comments on pulling data by looping through recordsets. I have a few questions still: First, the way the code is written the vba pulling data from two tables. My data is all in one table. How do I adapt this to search through just the one table. Secondly, my data is filtered in my query. Is it possible to have the code search through the query as opposed to the table? If not, how do I filter using vba? Lastly, keeping in mind question 2, how do I tell the code to do nothing if there are no results after being filtered?

    Basic structure
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
    .BodyFormat = olFormatRichText
    .To = "email address"
    ''.cc = ""
    ''.bcc = ""
    .Subject = "Active Projects Alert"
    .HTMLBody = "The following projects are approaching their deadlines:"
    .Send
    End With


    Recordset Code:
    Sub SendMail()
    Dim rsData As Recordset
    Dim rsEmails As Recordset
    Dim strBody As String
    Dim ...
    Set rsEmails = CurrentDb.OpenRecordset("SELECT Address FROM Staff;")
    While Not rsEmails.EOF
    Set rsData = CurrentDb.OpenRecordset("SELECT * FROM Data WHERE StaffID=" & rsEmails!StaffID & ";")
    While Not rsData.EOF
    'code to build email body using rsData record
    strBody = strBody & rsData!field1 & ", " & rsData!Field2 & ", " & rsData!field3 & vbCrLf
    rsData.MoveNext
    Wend
    'code to send email
    ...
    rsData.Close
    rsEmails.MoveNext
    Wend
    End Sub

    Thanks again for your help!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,953
    So the email addresses and user IDs and the data are all in one table? This would be unusual.

    The first recordset is to get a dataset of unique records for email and user ID. In your case, the first query could possibly be like:

    SELECT DISTINCT email, UserID FROM tablename;

    The code loops through this dataset and opens second recordset with a filter based on user ID so only data relevant to each user is retrieved and compiled into the email body.

    The EOF parameter prevents the looping code from executing if the dataset is empty. EOF stands for End Of File.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Email To Using Multiple Recipients
    By burrina in forum Sample Databases
    Replies: 1
    Last Post: 10-08-2019, 12:43 PM
  2. email multiple reports to multiple recipients
    By slimjen in forum Programming
    Replies: 5
    Last Post: 08-31-2014, 11:52 AM
  3. Replies: 3
    Last Post: 09-18-2013, 09:25 AM
  4. Replies: 2
    Last Post: 12-07-2011, 07:48 AM
  5. Replies: 2
    Last Post: 08-17-2010, 10:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums