Filtering Mailing Lists using Access and Outlook
Programming, Tips Tagged access, email, outlook, sending mail April 7th, 2009In what is becoming a series, we'll further tweak our code to allow for filtering of the query.
In the original code, we open a query directly as a recordset. This fails if the query requires some parameters.
(I'm not going to demonstrate a way to get user input and use that as the parameter. You should be able to copy and paste the code from the original user input sections of the code and modify as needed.)
To start, let's discuss the query and it's parameter.
In our original code, the query was just pulling a list of email addresses. For this, let's filter that list of addresses by domain.
(For our purposes, I'll assume you know how to write a query with parameters. Here's a screeenshot of how yours should look.)
You can see that we added a parameter called "domain" and the told Access that we want the query to return any record that has "domain" at the end.
So, when we run the query from the design window, we're prompted to enter the parameter:
and when we click RUN we'll get all email addresses from the jephens.com domain.
That's all well and good, but if we run the code as it exists, we get an error:
This occurs because the code doesn't know what to do with the parameter, so it punts and throws an error.
We'll need to handle this. And for that, we have to use a QueryDef object and assign a parameter.
So, instead of:
... we need to do a few things:
1. Create a QueryDef object
2 Assign our query to the object
3. Tell the object what our parameter is
4. Put the output of our query into our recordset.
Happily, this is pretty simple.
Set qryMail = currentdb.querydefs("MyEmailAddresses")
qryMail("domain") = ".com"
Set MailList = qryMail.OpenRecordset
And that will filter the recordset and only send mails to people in the .com namespace.
Of course, that's not the most user-friendly code, so you'd probably want to copy and paste some of the user input language and use that to fill in the parameter:
Domain$ = InputBox$("Please enter domain you would like to filter. Leave blank to send to everyone on the list.", "Users can be choosers!")
Set qryMail = currentdb.querydefs("MyEmailAddresses")
qryMail("domain") = domain$
Set MailList = qryMail.OpenRecordset
The nice thing about the way we wrote the query is we can leave the domain part blank, and the query will return all the records.



April 7th, 2009 at 3:48 pm
I cant see how to modify the code for my purpose. In my query I have two columns selected from a table, the email (of the eventual mailto) and the name (real name associated with the email address). The criteria for the query is in the name column and is Like[formname]![fieldname]. How can I code to replicate that?
Even if I just keep it simple and run the EmailAddresses query without any parameters and get its source from my other query, you still get the runtime error. Is there a simple way to not have to amend the code too much but allow the query based on a query to work?
April 8th, 2009 at 10:30 am
If you’re launching from a form, then you need to pass the form field to the routine.
The trick is NOT referring to the form field in your query, but in the code. The query needs a generic parameter (“domain”) in our example, and then you fill the generic parameter in with something specific.
So, the code would be something like:
Dim qryMail as QueryDef
Set qryMail = currentdb.querydefs(“MyEmailAddresses”)
qryMail(“domain”) =[formname]![fieldname]
Set MailList = qryMail.OpenRecordset
June 22nd, 2009 at 1:12 pm
Hi,
iam glad that i found you site which is really helpful to me .btw can you help in this below mentioned issue?
i have set up inventory stuff which has fields like :qty :500,id:1,supplier info:dcm,
and if user A enters information like qty to 100 id:2 supplier info:dcm
then current stock should display me 400 meaning 500-100 and it shoot email as soon as it gets current stock for all the users . iam using ms access 2007 and outlook 2007
Please get back to me on my email id .
looking forward to hear from you
Prasu
July 3rd, 2009 at 11:18 am
Prasu,
I don’t reply to emails directly since the blog is meant to be a learning resource, and I’d prefer everyone be able to share in my response.
The question is how/where is the user entering the “100″ which causes a display of “400″?
If it’s in a form, then you will need to kick off an e-mail to the applicable users in the form’s events. If it’s directly in a table, then you’re in for a lot more work since Access doesn’t support triggers (events which happen when data is manipulated directly at the table level) — so you’d have to write something that checked the tables every so often looking for a change and then kick off the email based on that.
I can’t offer much more specific help than that; but it’s a place to start looking.
July 30th, 2009 at 10:38 am
Please assist me on how to send an automated email to different users and attach a report that has filtered only that user’s information automatically, without sending other peoples information. I’m using MS Access 2007.
July 30th, 2009 at 1:24 pm
You would need to call a query each time through the loop.
So you have the first query which provides all the email addresses, then you’d need to run a second query which uses information (UserID, email address, whatever) from the first quer to pull out whatever information you need.
Unfortunately, this isn’t something I’m able to write up in great detail right now, but I’ll put it on the list for a new article.