How to modify vba code in MS Access to send to all email addresses in a form, not just the first one?

The below code works great with the exception of when “Forms!frmFOL![FOL Email]” contains more than 1 email address/record.  When there are multiple records in the form, it only sends it to the email address in the first record only.  How can I make it include all of the records in that form?

Option Compare Database

'------------------------------------------------------------
' RunDailyReport
'
'------------------------------------------------------------
Function RunDailyReport()
On Error GoTo RunDailyReport_Err

    DoCmd.SetWarnings False
    DoCmd.OpenForm "frmFOL", acNormal, "", "", , acNormal
    DoCmd.OpenForm "frmFSL", acNormal, "", "", , acNormal
    DoCmd.OpenForm "frmROM", acNormal, "", "", , acNormal
    DoCmd.SendObject acReport, "rptDailyReport", "XPSFormat(*.xps)", Forms!frmFOL![FOL Email] & "; " & Forms!frmFSL![FSL Email], Forms!frmROM![ROM Email] & "; " & "FSQM@nmci-isf.com", "", "Daily Audit", "Here are the results for today's audit.  Please let me know if you have any questions." & vbCrLf & vbCrLf & "Field Service Queue Management" & vbCrLf & "My Company Name" & vbCrLf & "Email: FSQM@domain", True, ""
    DoCmd.Close acForm, "frmROM"
    DoCmd.Close acForm, "frmFSL"
    DoCmd.Close acForm, "frmFOL"


RunDailyReport_Exit:
    Exit Function

RunDailyReport_Err:
    MsgBox Error$
    Resume RunDailyReport_Exit

End Function

Open in new window

kbay808Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LajuanTaylorCommented:
Is your form using a semicolon as the delimiter between multiple email addresses?
0
kbay808Author Commented:
I'm not sure how to do that.  Please see the attached screen shot of my form.
Form-ScreenShot.jpg
0
kbay808Author Commented:
I tried adding a semicolon to the end of the email addresses in the query that the form is pulling from and it appears in the form, but the code failed and the email was not created.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

LajuanTaylorCommented:
Okay, I see... In the VBA you need to iterate over the email list if it there's more than one email address. The following code comes from the web site: http://www.access-diva.com/vba16.html

I think you should be able to modify the code to work within your report sub routine. See link above for more details.

Private Sub SendeMail()
     Dim rs As Recordset
     Dim vRecipientList As String
     Dim vMsg As String
     Dim vSubject As String

          Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryYourQueryWitheMailAddresses ")
          If rs.RecordCount > 0 Then
              rs.MoveFirst
              Do
                  If Not IsNull(rs! YoureMailAddressField ) Then
                      vRecipientList = vRecipientList & rs! FieldThatHoldsTheeMailAddresses & ";"
                      rs.MoveNext
                 Else
                      rs.MoveNext
                 End If

             Loop Until rs.EOF

             vMsg = " Your Message here... "
             vSubject = " Your Subject here... "

             DoCmd.SendObject acSendReport, " rptYourReport ", acFormatPDF, vRecipientList, , , vSubject, vMsg, False
             MsgBox ("Report successfully eMailed!")

     Else
             MsgBox "No contacts."
     End If
End Sub

Open in new window

0
kbay808Author Commented:
I tried every way that I could think of but I can't seem to get it right.  The name of the query is "qryFOL" and the field is "NMCI Email".
Error---Debug-Screen-Shot-1.jpg
Error---Debug-Screen-Shot-2.jpg
0
LajuanTaylorCommented:
Hi,  I made a slight change to the loop in the sample. I uploaded a sample database with a file name extension of .txt, just change it to ".accdb".

There's caveat with the record set query which, I explain in the attached screen shot.

Code Snippet:
Sub send_email_using_query()
     Dim rs As Recordset
     Dim vRecipientList As String
     Dim vMsg As String
     Dim vSubject As String

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryFOL ")
    vRecipientList = ""
    While Not rs.EOF
        vRecipientList = vRecipientList & rs(1) & ";"
        rs.MoveNext
    Wend
    rs.Close
    Set rs = Nothing

             vMsg = " Your Message here... "
             vSubject = " Your Subject here... "
             DoCmd.SendObject acSendForm, "sales_detail", acFormatPDF, vRecipientList, , , vSubject, vMsg, True
             MsgBox ("Report successfully eMailed!")
    
End Sub

Open in new window

SampleSendMailDB.txt
2015-04-01-0036-SendMailToMultipleEmails
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
This is generally a bad idea:

 While Not rs.EOF
        vRecipientList = vRecipientList & rs(1) & ";"
        rs.MoveNext
    Wend

You should instead explicitly refer to the name of the field that holds the Email addresses, instead of assuming it's going to be in a specific location in the recordset:

While Not rs.EOF
  vRecipientList = vRecipientList & rs("NameOfEmailField") & ";"
  rs.MoveNext
Wend
0
LajuanTaylorCommented:
@Scott McDaniel (Microsoft Access MVP - EE MVE ) - Thanks for the tip. Are there situations when this approach is useful?

I deal with data sets frequently that have positional layouts, but that's mainframe and iSeries transmissions...
0
kbay808Author Commented:
I'm still get an error.  Please see the attachments for details.  I also attached a screen shot of the query.  It only has one field.
Error-Screen-Shot.jpg
Debug-screen-shot.JPG
Screen-Shot-of-Query.jpg
0
LajuanTaylorCommented:
If your query field name is "NMCI Email", use that instead of "0". Sorry for the confusion.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Queries in Access can easily be rewritten (and very often are), so using positional syntax to refer to a specific field in an Access query is not the best method to use. Not to say it won't work - it will - but it's far too simple for user to modify a query. Fieldnames don't change often, of course, so referring to distinctly to a Field is the preferred way of handling things in the Access world.

"Too Few Parameters" often means you've misspelled the query name, or the name of a field. In your case, you have a space after the query name in your OpenRecordset statement.

If that doesn't work, then try Compacting your database ( make a copy first).
0
LajuanTaylorCommented:
@Scott McDaniel (Microsoft Access MVP - EE MVE ) - Thanks.
0
kbay808Author Commented:
I tried with and without the space after the query name after compacting the DB, but I'm still getting the same error for the same line.
0
LajuanTaylorCommented:
My query "qryFOL" SQL looks like:
SELECT tblContacts.Email
FROM tblContacts; 

Open in new window



My VBA Module looks like:
Sub send_email_using_query()
     Dim rs As Recordset
     Dim vRecipientList As String
     Dim vMsg As String
     Dim vSubject As String

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryFOL ")
    vRecipientList = ""
    While Not rs.EOF
        vRecipientList = vRecipientList & rs("Email") & ";"
        rs.MoveNext
    Wend
    rs.Close
    Set rs = Nothing

             vMsg = " Your Message here... "
             vSubject = " Your Subject here... "
             DoCmd.SendObject acSendForm, "sales_detail", acFormatPDF, vRecipientList, , , vSubject, vMsg, True
             MsgBox ("Report successfully eMailed!")
    
End Sub

Open in new window

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Is the table field which holds the email named "FOL Email". If so, then do this:

While Not rs.EOF
        vRecipientList = vRecipientList & rs("FOL Email") & ";"
        rs.MoveNext
Wend

You'll have to make sure the correct value is being referred to in the statement above. We don't have your database, so we can't pinpoint the exact field name. As mentioned, the error you're getting means you're referring to a field or table that does not exist (i.e. you've got the name wrong).

It would also help greatly if you'd show the SQL of "qryFOL"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbay808Author Commented:
I'm still getting the same error for the below line.
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryFOL")

Open in new window


The field name in the query is named "NMCI Email".  Here is the SQL for the qryFOL

SELECT [qryContacts(FSL,FOL,ROM)].[NMCI Email]
FROM [qryContacts(FSL,FOL,ROM)]
WHERE ((([qryContacts(FSL,FOL,ROM)].Title)=[forms]![Daily Report]![SiteGroupInput]) AND (([qryContacts(FSL,FOL,ROM)].Role) Like "fol"));

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.