Solved

How send email using first record in query

Posted on 2013-10-24
1
362 Views
Last Modified: 2013-10-30
I have this code in the onload event of a form.  When the form is loaded an email is automatically send to recipients that come from a query.  But now I only want ONE email to be send per unique InvoiceN.  How does this code have to be changed to do this?

Private Sub Form_Load()
   Dim strSQL As String
   Dim rs As DAO.Recordset
   Dim strEmail As String, strSubject As String, Customer As String, InvoiceN As String, strToAddress As String, ToEmail As String

   strSubject = "The afore mentioned customer's contract is up for renewal. Please arrange to send the invoice."
   Set rs = CurrentDb.QueryDefs("qryFindRecordsForAutoEmail").OpenRecordset

   While Not rs.EOF

        If IsNull(rs!ccEmpEmailAddresses) Or IsNull(rs!EmailAddress) Then
       
            MsgBox "Customer " & rs!Customer & ", Record ID " & rs!ID & ", is missing either the Email Address or the c/c Email Address!"

        Else
            Customer = rs!Customer
            InvoiceN = rs!InvoiceN
            strEmail = rs!ccEmpEmailAddresses
            ToEmail = rs!EmailAddress
            strToAddress = rs!ccEmpEmailAddresses
           
            DoCmd.SendObject acSendNoObject, , , ToEmail, strToAddress, , strSubject, "Customer: " & [Customer] & " - Invoice Number: " & [InvoiceN], False
           
            rs.Edit
            rs![EmailReminderSent] = True
            rs![EmailSentDate] = Date
            rs.Update
        End If

        rs.MoveNext
    Wend
    rs.Close
    Set rs = Nothing

End Sub
0
Comment
Question by:SteveL13
1 Comment
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 39598256
You need to iterate through the recordset, creating a separate email for each member.  Here is some code that does this, using the more reliable Automation method:

Public Sub EMailAllContacts()
'Created by Helen Feddema 31-Oct-2009
'Last modified by Helen Feddema 31-Oct-2009

On Error GoTo ErrorHandler

   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset
   Dim appOutlook As Outlook.Application
   Dim msg As Outlook.MailItem
   Dim strEmail As String
   
   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("qryContacts")
   Set appOutlook = GetObject(, "Outlook.Application")
   
   Do While Not rst.EOF
      strEmail = Nz(rst![EmailName])
      If strEmail <> "" Then
         'Create email
         Set msg = appOutlook.CreateItem(olMailItem)
         msg.To = strEmail
         msg.Subject = "Subject"
         msg.Body = "Message"
         
         'Comment out next line and uncomment Send line
         'to send automatically
         msg.Display
         'msg.Send
      End If
      rst.MoveNext
   Loop
   
ErrorHandlerExit:
   rst.Close
   Set rst = Nothing
   Set appOutlook = Nothing
   Exit Sub

ErrorHandler:
   'Outlook is not running; open Outlook with CreateObject
   If Err.Number = 429 Then
      Set appOutlook = CreateObject("Outlook.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number _
         & " in EMailAllContacts procedure" _
         & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

End Sub

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now