Solved

E-Mail Group from query of contact records in Access 2007

Posted on 2009-04-09
15
260 Views
Last Modified: 2012-05-06
I am working on creating a form (frmEmail) where users can select options (txtPRN, lstFAC, cboPrimary) to create an e-mail group.  We typically a forwarding messages so I am thinking it will be easiest to create the e-mail group/list in a text box that they can copy and paste to the e-mail that needs to be forwarded.  I have a query (qryEmail) built that pulls the correct records but am not sure how to get the data from the query result to the text box

I found an old question that seems to contain nearly the exact scenario but it is from 2004 and I get an error on the message.  Can someone please help me update this code so that it will work for me?  I don't need the additional fields shown in the sample text, only the one text field with the e-mails.

The error message that I get is on the   Set MyRS = MyDb.OpenRecordset("qryEmail", dbOpenDynaset).  The error message is Run Time Error 3061. Too Few Parameters. Expected 3.

Thank you.
Dim MyDb As DAO.Database
Dim MyRS As DAO.Recordset
Dim strTo As String
Dim strCC As String
Dim strBCC As String
Dim strMsg As String
Dim strSubject As String
 
Set MyDb = CurrentDb
Set MyRS = MyDb.OpenRecordset("qryEmail", dbOpenDynaset) 'This is the recordset
MyRS.MoveFirst
Do
 
strTo = MyRS("Email")              'THIS IS THE EMAIL FIELD
strMsg = "Message "             'This is the mail message
strSubject = "Subject"         'This is the subject
DoCmd.SendObject acReport, "ReportName", acFormatXLS,strTo, strCC, strBCC, strSubject, strMessage,val(0)          ' send mail
MyRS.MoveNext
Loop Until MyRS.EOF
MyRS.Close
MyDb.Close

Open in new window

0
Comment
Question by:Melissa-FAC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 13

Expert Comment

by:lucas911
ID: 24110375
What does your qryEmail look like?  You should try creating the sql in code:
Set MyDb = CurrentDb
Set MyRS = MyDb.OpenRecordset("select * from qryEmail where [yourmailtogroup]='" & Me.TxtMailToGroup & "'", dbOpenDynaset)
 
'Something like that ... otherwise you have to pass some parameters over.

Open in new window

0
 

Author Comment

by:Melissa-FAC
ID: 24110482
Here is my query:

SELECT tblContacts.Email
FROM tblContacts
WHERE (((tblContacts.FAC) Like "*" & [Forms]![frmEMail]![lstFAC] & "*") AND ((tblContacts.PRN) Like "*" & [Forms]![frmEMail]![txtPRN] & "*") AND ((tblContacts.Primary) Like "*" & [Forms]![frmEMail]![cboPrimary] & "*"));
0
 
LVL 13

Expert Comment

by:lucas911
ID: 24110609
So then your SQL would look something like this.
Set MyDb = CurrentDb
Set MyRS = MyDb.OpenRecordset("select * from tblContacts where FAC like *'" & [Forms]![frmEMail]![lstFAC] & "' * and PRN like *'" & [Forms]![frmEMail]![txtPRN] & "'* and Primary Like *'" &  [Forms]![frmEMail]![cboPrimary] & "'*")

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:Melissa-FAC
ID: 24110750
I used your SQL statement but received the following message:  Run time error 3075.  Syntax Error (missing operator) in query expression 'FAC like *'Doug'* and PRN like *'910'* and Primary Like *"*'.  Doug and 910 being the parameters I used.

I attached what I used.
Set MyRS = MyDb.OpenRecordset("select * from tblContacts where FAC like *'" & [Forms]![frmEMail]![lstFAC] & "' * and PRN like *'" & [Forms]![frmEMail]![txtPRN] & "'* and Primary Like *'" & [Forms]![frmEMail]![cboPrimary] & "'*")

Open in new window

0
 
LVL 13

Expert Comment

by:lucas911
ID: 24110901
910 looks like a number.  What is [Forms]![frmEMail]![cboPrimary]?  I created 2 sql statements depending if you need cboprimary or not.
If not isnull([Forms]![frmEMail]![cboPrimary]) then
  Set MyRS = MyDb.OpenRecordset("select * from tblContacts where FAC 
  like *'" & [Forms]![frmEMail]![lstFAC] & "' * and PRN like *" & 
  [Forms]![frmEMail]![txtPRN] & "* and Primary Like *'" & [Forms]!
  [frmEMail]![cboPrimary] & "'*")
else
  Set MyRS = MyDb.OpenRecordset("select * from tblContacts where FAC 
  like *'" & [Forms]![frmEMail]![lstFAC] & "' * and PRN like *" & 
  [Forms]![frmEMail]![txtPRN] & "*"")
end if

Open in new window

0
 

Author Comment

by:Melissa-FAC
ID: 24111224
I still get the same error message - now on both statements.  cboPrimary is a Yes/No field.  I actually converted it to a checkbox onthe form but did not change the name of the field since I already had it in my queries.
0
 
LVL 13

Expert Comment

by:lucas911
ID: 24111307
So then the last part can't be like yes or no.
  Set MyRS = MyDb.OpenRecordset("select * from tblContacts where FAC 
  like *'" & [Forms]![frmEMail]![lstFAC] & "' * and PRN =" & 
  [Forms]![frmEMail]![txtPRN] & " and Primary =" & [Forms]!
  [frmEMail]![cboPrimary] & "")

Open in new window

0
 

Author Comment

by:Melissa-FAC
ID: 24111383
I am sorry.  I do not understand this statement.  I am still getting the same error message.  I have a query that pulls the correct records.  Is it possible to run the query in hidden mode or something and just pull the e-mail addresses from there?
0
 
LVL 13

Expert Comment

by:lucas911
ID: 24129454
Melissa, here is one with parameters.

1.  Go to your qryEmail and create these parameters (right click anywhere on the gray part and select parameters)
[Forms]![frmEMail]![lstFAC]            (VALUE)
[Forms]![frmEMail]![txtPRN]           (VALUE)
[Forms]![frmEMail]![cboPrimary]    (VALUE)

2.  I'm assuming your criterias have already been setup like so:
like *[Forms]![frmEMail]![lstFAC]*

The code below, creates 3 parameters and passess them over to the query.  Then i simply create a recordset based on the query's sql.  If this doesn't work then attach an empty database and i'll make it work for you.
Dim qd As DAO.QueryDef
Dim myrs As DAO.Recordset
Set qd = CurrentDb.QueryDefs("qryEmail")
 
qd(0) = [Forms]![frmEMail]![lstFAC]
qd(1) = [Forms]![frmEMail]![txtPRN]
qd(2) = [Forms]![frmEMail]![cboPrimary]
 
Set myrs = CurrentDb.OpenRecordset(qd.sql)
 
myrs.MoveFirst
Do
strTo = myrs("Email")              'THIS IS THE EMAIL FIELD
strMsg = "Message "             'This is the mail message
strSubject = "Subject"         'This is the subject
DoCmd.SendObject acReport, "ReportName", acFormatXLS, strTo, strCC, strBCC, strSubject, strMessage, Val(0)        ' send mail
myrs.MoveNext
Loop Until myrs.EOF
 
Set qd = Nothing
myrs.Close
Set myrs = Nothing

Open in new window

0
 

Author Comment

by:Melissa-FAC
ID: 24130732
I tried but still received errors.  Attached is my sample database with the related items.  Your help is much appreciated!
FAC-DB-Sample.mdb
0
 
LVL 13

Expert Comment

by:lucas911
ID: 24131158
ok take a look at it.

I made some changes but you can read through them in my comments.
FAC-DB-Sample.mdb
0
 

Author Comment

by:Melissa-FAC
ID: 24131498
This works but I don't think it does what I need.  I need a text box with the e-mail addresses separated by semi colons so the addresses can all be copied and pasted into the BC field of an existing e-mail which is being forwarded.  Is that possible with this list box?
0
 
LVL 13

Accepted Solution

by:
lucas911 earned 500 total points
ID: 24131587
i added a bcc textbox.
FAC-DB-Sample.mdb
0
 

Author Comment

by:Melissa-FAC
ID: 24131625
That's it!!!  Thank you!
0
 

Author Closing Comment

by:Melissa-FAC
ID: 31568678
Thank you!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

688 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