kbay808
asked on
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
Is your form using a semicolon as the delimiter between multiple email addresses?
ASKER
I'm not sure how to do that. Please see the attached screen shot of my form.
Form-ScreenShot.jpg
Form-ScreenShot.jpg
ASKER
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.
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.
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
ASKER
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
Error---Debug-Screen-Shot-1.jpg
Error---Debug-Screen-Shot-2.jpg
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:
2015-04-01-0036-SendMailToMultipleEmails
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
SampleSendMailDB.txt2015-04-01-0036-SendMailToMultipleEmails
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
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
@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...
I deal with data sets frequently that have positional layouts, but that's mainframe and iSeries transmissions...
ASKER
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
Error-Screen-Shot.jpg
Debug-screen-shot.JPG
Screen-Shot-of-Query.jpg
If your query field name is "NMCI Email", use that instead of "0". Sorry for the confusion.
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).
"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).
@Scott McDaniel (Microsoft Access MVP - EE MVE ) - Thanks.
ASKER
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.
My query "qryFOL" SQL looks like:
My VBA Module looks like:
SELECT tblContacts.Email
FROM tblContacts;
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm still getting the same error for the below line.
The field name in the query is named "NMCI Email". Here is the SQL for the qryFOL
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryFOL")
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"));