Solved

Send email based on birth month

Posted on 2015-01-16
15
141 Views
Last Modified: 2016-02-10
I am using Access 2013 and need help with send an email based on birth month.

I have a table with all employees and need a ( macro or vba ) button that when pressed will send a seperate email to each person for the current birth month. I also need to place some generic text and subject for each email.
0
Comment
Question by:CMILLER
  • 9
  • 5
15 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40553943
using recordset..


Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
dim rs as dao.recordset, strText, strSubj, strTo
set rs=currentdb.openrecordset("select * from tblEmployees where month([birthdate])=Month(Now())")

do until rs.eof
   strTo=rs!Email
   strText="<some text>"
  strsubj="<subject>"

oMail.Body = strText
oMail.Subject = strsubj
oMail.To = strTo
oMail.Send


rs.movenext
loop

Set oMail = Nothing
Set oApp = Nothing
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40554030
<No Points wanted>
You may encounter the Outlook security popup.
If so, then you can use this utility to bypass/suppress it.
http://www.contextmagic.com/express-clickyes/
0
 

Author Comment

by:CMILLER
ID: 40554588
Having an issue at:  dim rs as dao.recordset, strText, strSubj, strTo
0
 

Author Comment

by:CMILLER
ID: 40554592
I fixed it. Added from the Ref Lib.

Now I get a run-time error 3131: syntax error in FROM clause
0
 

Author Comment

by:CMILLER
ID: 40554596
My table name has spaces. Created a temp local with no spaces in the table name.

Now I get an run-time error 287 at oMail.Send
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40554597
@CMILLER
without seeing what codes you  are using it will be difficult to know why you are getting the error
0
 

Author Comment

by:CMILLER
ID: 40554606
Option Compare Database
Option Explicit

Function ExportBirthMonthEmail()


Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
Dim rs As DAO.Recordset, strText, strSubj, strTo
Set rs = CurrentDb.openrecordset("select * from DCPDSMilitaryDataDOBEmail where month([DOB])=Month(Now())")

Do Until rs.EOF
strTo = rs!Email
strText = "<some text>"
strSubj = "<subject>"

oMail.Body = strText
oMail.Subject = strSubj
oMail.To = strTo
oMail.Send


rs.movenext
Loop

Set oMail = Nothing
Set oApp = Nothing

End Function
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40554613
try replacing

oMail.Send

with

oMail.Display
0
 

Author Comment

by:CMILLER
ID: 40554621
That worked and it sent an email but it only sent one email, which was to the last person in the table. it should have sent three seperate emails.
0
 

Author Comment

by:CMILLER
ID: 40554655
The user has decided that they want one email sent with all employees that have a birthday in the current.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40554734
try this then

Dim oApp As Outlook.Application
 Dim oMail As MailItem
 Set oApp = CreateObject("Outlook.application")
 Set oMail = oApp.CreateItem(olMailItem)
 Dim rs As DAO.Recordset, strText, strSubj, strTo
 Set rs = CurrentDb.openrecordset("select * from DCPDSMilitaryDataDOBEmail where month([DOB])=Month(Now())")

 strText = "<some text>"
 strSubj = "<subject>"

 Do Until rs.EOF
      strTo = strTo & ";" & rs!Email

 rs.movenext
 Loop
 
 strTo=mid(strTo,2)

 oMail.Body = strText
 oMail.Subject = strSubj
 oMail.To = strTo
 oMail.display


 Set oMail = Nothing
 Set oApp = Nothing
0
 

Author Comment

by:CMILLER
ID: 40560771
That Worked. Thanks!
0
 

Author Comment

by:CMILLER
ID: 40589095
Obrero,

Hi, quick question.

For the strText = "<some text>", How do I make it multiple paragraphs with spaces in between the paragraphs?

Thanks.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40589145
strText="The quick brown fox" & vbcrlf & vbcrlf
strtext=strtext & "jump over the" & vbcrlf & vbcrlf
strtext=strtext & "lazy sleeping dog"
0
 

Author Comment

by:CMILLER
ID: 40589917
ahh....cool thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now