Access 2010 - Send email based on date field (1 Viewer)

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi There Access Experts

i really need some help here.

i am busy with a new database with a form based on a query where i have a field called [Follow_Up_With_Customer] this is a date field.

i need an email to be sent to all customer on the date given in this field but only if field [Plans_Received] is not checked "yes"

but before the email is sent a list of all these customers must pop up "field [Customer_Name_and_Surname]" with a promt asking is it okay to send the mail to them.

once the mail has been sent it must not send the same mail to the customer again when opening the database.

your assistance will be greatly appreciated.

and dont hesitate to let me know if more info is needed.

i have looked everywhere on the internet and cant find anything that will help me with this.

the vba must run when my form is opened.
 

isladogs

MVP / VIP
Local time
Today, 13:08
Joined
Jan 14, 2017
Messages
18,216
Welcome to AWF. For info, I moved your post from the Introduce Yourself area as it is a question about code

To prevent duplication, add another Boolean field EmailSent.
When the button is clicked to send email, use an update query to update that field to True

To identify which emails to send, you need a query filtering for:
- the selected date in the date field
- Received field = false.
- EmailSent = False
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi there

thank you for getting back to me and sorry about the post in the wrong place. i am new to this website.

i have added the EmailSent however mine is just called Follow_Up_Email_Sent

is there any possibility that you could help me put this vba code together

i have my query filtering for the following as per your post.

- the selected date in the date field
- Received field = false.
- EmailSent = False

Thank you
 

isladogs

MVP / VIP
Local time
Today, 13:08
Joined
Jan 14, 2017
Messages
18,216
Post the SQL for your select query.
I also need the table name and the button name
Recommend you use shorter field names, using CamelCase and without underscores.
EmailSent is less to type than what you have.
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
Several questions that may help you define exactly how the code should work;
1) Are you trying to send 1 Email to a group of people OR send each an individual Email?
2) Do you want to see each Email before it is sent or have it automatically go?
3) Will you need to resend if there is an issue with one (or more) of the Email addresses sent to?
4) Are you in charge of the Email server you are using?
5) What volume of Emails will you be sending? If this is only a dozen or so at once you should not have to worry about being identified as a spammer. If it is thousands at one go, you will probably want to send out small batches periodically.
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi Isladogs

please see below my SQL

SELECT NewCustomerDetails.ID, NewCustomerDetails.Date_Of_Site_Visit, NewCustomerDetails.Customer_Name_and_Surname, NewCustomerDetails.Customer_Cell_Number, NewCustomerDetails.[Customer_E-Mail_Address], NewCustomerDetails.Customer_Address, NewCustomerDetails.Phoned_Customer, NewCustomerDetails.Date_Phoned, NewCustomerDetails.Action_To_Be_Taken, NewCustomerDetails.Plans_Received, NewCustomerDetails.Quote_Done, NewCustomerDetails.Follow_Up_With_Customer, NewCustomerDetails.Notes, NewCustomerDetails.Send_Info_EMail_To_Customer, NewCustomerDetails.Follow_Up_On_Quote_Done, NewCustomerDetails.Quote_Done_Date, NewCustomerDetails.Follow_Up_Email_Sent
FROM NewCustomerDetails;


Then the table name is NewCustomerDetails

Sorry about the field names... i will remember this going forward.

thank you for your help.

DeWaal
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi Mark

Please see the answers to your questions below.

1) Are you trying to send 1 Email to a group of people OR send each an individual Email?
- That is correct it is one email to a group of people

2) Do you want to see each Email before it is sent or have it automatically go?
- The emails can be sent automatically yes. but before it send it must give me a list of all the customer names that it will be going to and on that pop up list there must be a button saying something like - are you sure you want to send the mails with a yes and no button

3) Will you need to resend if there is an issue with one (or more) of the Email addresses sent to?
- indeed this will be good to have

4) Are you in charge of the Email server you are using?
- i am not in charge of the server no. however there wont be allot of emails that get sent a day.

5)What volume of Emails will you be sending? If this is only a dozen or so at once you should not have to worry about being identified as a spammer. If it is thousands at one go, you will probably want to send out small batches periodically.
- as i mentioned above it wont be allot. it i dont even thing that it will be more than 20 a day.

thank you for your effort Mark
 

isladogs

MVP / VIP
Local time
Today, 13:08
Joined
Jan 14, 2017
Messages
18,216
Your update query will need to be something like
Code:
UPDATE NewCustomerDetails SET NewCustomerDetails.Follow_Up_Email_Sent = True
WHERE (((NewCustomerDetails.[B][COLOR="Red"]YourDateField[/COLOR][/B])=[COLOR="red"]Date()[/COLOR]));

Modify the parts in RED as appropriate

Add this to the button click event code either as a query or as SQL
I'll leave Mark to respond to the points raised in your separate exchange
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
For your list of customers, I'd suggest, in effect, opening the query you use in datasheet mode so you can review. You will want to move this to a continuous form so you have better control over what is being displayed and setting up how you want to allow specific customers to be removed.

From a users perspective, you want to show them "Here is who you will send to, press button to send". You will also want the same form to show who you have already sent to (Colin has shown how) so that you can clear the date you sent so they can be resent to. This would normally also include showing the Email address you sent to and their other contact information. This will let the end users fix any bad Email addresses you have.

You may also want to have a "Maximum Emails in list" option. Depending on your set up, having too many addresses on one email may cause issues in the future. We are HOPING this will be something useful enough that you will have more than 20 emails a day... eventually.

If you need the actual code to look through a query and build the address list or the code to send an email, let me know.
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Thank you Mark

Thank you for getting back to me.

okay i am not an expert at this. could you help me with some vba code to take me in the right direction.

like i explained. when my form frmNewCustomerDetails is opened the vba code must run automatically, and basically do what we have discussed.

1. on open of the form a pop up stipulating like you said "Here is who you will send to, press button to send"

2. the email must be sent to all customer with date fields [Follow_Up_With_Customer] corresponding with today's date providing the field [Plans_Received] is not checked to "Yes"

3. And then last thing the email must not be sent again when it has already been sent. and i have added the "EmailSent" field - mine is just called [Follow_Up_Email_Sent]
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
I would have the form open your review form itself. On the review form, put on two buttons. The first simply quits. The second sends the Emails, updates to say they were sent (as Colin suggests) and quits.

The actual code will look a lot like the following if you are using Outlook;

Code:
Private Sub btn_Send_Click()
   Dim asSQL As String
   Dim rs As DAO.Recordset
   Dim afError As Byte
   Dim asError As String
   Dim asEmail As String
   Dim asSep As String
   Dim asClass As String
      
   
   'Begin going through the students by class, matching selected class.
   asEmail = ""
   asSep = ""
   asSQL = "SELECT EmailAddress FROM Q_CustomersToEmail"
   Set rs = CurrentDb.OpenRecordset(asSQL)
   
   'Check to see if the recordset actually contains rows
   If Not (rs.EOF And rs.BOF) Then
       rs.MoveFirst 'Unnecessary in this case, but still a good habit
       Do Until rs.EOF = True 
         asEmail = asEmail & asSep & rs("EmailAddress") 
         asSep = “; “
          'Move to the next record. Don't ever forget to do this.
          rs.MoveNext
       Loop
   Else
       MsgBox "There are no customers who need to be Emailed"
   End If
   
   'MsgBox "Emailng to " & asEmail
   
   Send_Email (asEmail)

   rs.Close 'Close the recordset
   Set rs = Nothing 'Clean up
End Sub

Private Sub Send_Email(pvMailAddress As Variant)

    Dim EmailApp As Outlook.Application
    Dim NameSpace As Outlook.NameSpace
    Dim Folder As Outlook.Folder
    Dim EmailSend As Outlook.MailItem
          
    'MsgBox "Sending EMail to " & pvMailAddress
        
    'create email
    Set EmailApp = New Outlook.Application
    Set NameSpace = EmailApp.GetNamespace("MAPI")
    Set Folder = NameSpace.GetDefaultFolder(olFolderInbox)
    Set EmailSend = Folder.Items.Add(olMailItem)
    
    With EmailSend
        .To = pvMailAddress
        ‘.BCC = pvMailAddress – If you wanted to send as a blind CC instead.
        .Subject = <<YOUR SUBJECT HERE>>
        .HTMLBody = <<YOUR BODY HERE>>
        End If
        .Display  ‘SHOWS what the Email will look like – for testing.
        '.Send  ' Uncomment when this is working the way you want.
    End With
End Sub
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
Now the source for who you send to is a different issue.
You will want to make a query that return JUST the records you need to Email. In my previous post I am referencing it as Q_CustomersToEmail. Let us know if you need help working out how to make a query that returns ONLY the customers you need.

Since there are two different parts I figure make two posts so you can more easily reference each.
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi Mark

Okay, i created the form "frmReviewForm" as per your request with the 2 buttons.

Button 1 - Cancel = Macro to close form "frmReviewForm"
Button 2 - Send Mail = I am not to sure what you need me to do here

Then you mentioned that i make a query that returns ONLY the customers i need. how do i do this? - i have created the query "qryMailCustomerList" however i am not sure how to get it to return only the customers i need.

keeping in mind that it must email all customer where field [Follow_Up_With_Customer] matches today's date, where field checkbox [Plans_Received] is checked "No" if "Yes" the mail must not be sent to that customer.

hope you understand.

thank you so very much for your help mark.
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
DeWaal,

I am guessing you are fairly new to ACCESS, correct?

The code I posted is sample VBA code for the 2nd button's onclick event.

Lets start with the query, do you know how to set up queries in ACCESS that are limited to specific records?
 

DeWaal

New member
Local time
Today, 05:08
Joined
Feb 20, 2019
Messages
7
Hi Mark

I am quite new to Access VBA yes.

I have managed to create the query and it now only displays the customers that needs to be mailed.

I have placed your code on the send email buttons one click event like you have explained below.

What is our next step.
 

Mark_

Longboard on the internet
Local time
Today, 05:08
Joined
Sep 12, 2017
Messages
2,111
Double check to make sure that the code works based on your query. I am guessing you are using different names that I did, so you will have to modify it to use the names of your query and your fields.

Then you get to work out how to generate the subject for the email and how you want to fill in the body of the email.

Once it is working and you see the email you want displayed with the right list of customers, you can comment out the .display and enable the .send

After that, once you know the program will send the Emails you want, comment out the call to Send_Email so you can get the part working where you mark the records as having been sent. After that is working enable Send_Email again and (hopefully) you are done!
 

Users who are viewing this thread

Top Bottom