Results 1 to 11 of 11
  1. #1
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214

    Send Emails Automatically

    I know there are a million different threads out there concerning this topic but I am looking for some more add-ons to the current code below.



    Code:
    Private Sub SendEmail_Click()
    On Error GoTo SendEmail_Err
        Dim myOlApp As Object
        Dim myNameSpace As Object
        Dim myFolder As Object
        Dim myItem As Object
        Dim myAttachments, myRecipient As Object
        Dim recipient As String
        Dim file_name As String
        Dim mySubject As Object
        Dim dbs As Object
        Dim rst As Object
        Dim strSQL As String
        
        strSQL = "Query1"
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset(strSQL)
        rst.MoveFirst
        While Not rst.EOF
            recipient = rst!myemail
            Set myOlApp = CreateObject("Outlook.Application")
            Set myItem = myOlApp.CreateItem(olMailItem)
            Set myAttachments = myItem.Attachments
            Set myRecipient = myItem.Recipients.Add(recipient)
            '"Message Subject String Here"
            myItem.Subject = "Test"
            '"Put Message Body Text Here"
            myItem.Body = rst!myname & "," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "You are approaching the Expiration/Renewal Date for the following: " & rst!mydate & _
            myItem.Display
            myItem.Send
            rst.MoveNext
        Wend
        Set myRecipient = Nothing
        Set myAttachments = Nothing
        Set myItem = Nothing
        Set myOlApp = Nothing
        Set rst = Nothing
    SendEmail_Exit:
        Exit Sub
    SendEmail_Err:
        MsgBox Err.Description
        Resume SendEmail_Exit
    End Sub
    Here is a little bit about my table:

    Table: Reps (myname)
    Fields: DueDate - Date/Time (mydate)
    ExpirationDate (for ex NJ) - Date/Time
    ExpirationDate (for ex Pa) - Date/Time

    Basically, I have a table that has multiple fields with different expiration dates. I have a query set up that pulls the dates that are from today to 30 days in the future. Any of the info in that query is used to generate the emails going out.

    SOLVED Issue 1:
    I want to only have the emails sent out one time when they first meet the criteria of the query. I have tried this in my code to have the query update the table but I can't get it to work.
    Code:
    myItem.Display
    myItem.Send
    .Edit
    Emailed = True 'This is a Yes/No field in the Table that I want to have updated so that once the email has been sent for that specific date, it no longer shows in the query and thus is not emailed out again.
    .Update
    rst.MoveNext
    Wend
    SOLVED Issue 2:
    I can't figure out how to get the field name to show up before the date. For example, I want it to look like this: You are approaching the Expiration/Renewal Date for the following: NJ 10/20/2012.
    I can get the date to show up but not the field name.

    I think that's enough for now. Any help would be appreciated.
    Last edited by cbrsix; 10-19-2012 at 09:57 AM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't see that you have a With block running and you didn't specify the recordset field. Try

    Code:
    rst.Edit 
    rst!Emailed = True 
    rst.Update
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    Thank you, that worked. I knew it was something stupid I was missing.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    I mentioned before that I have multiple columns with dates in each one. My dillema now is that when the first Emailed is updated to True, the only way that I could think of getting rst!mydate to change to the next date (mydate2) was to add an iif() statement.
    Code:
     myItem.Body = rst!myname & "," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "You are approaching the Expiration/Renewal Date for the following: " & IIf(rst!Emailed = True, rst!mydate2, rst!mydate) & _
    Is this the best way to do this?

  6. #6
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    Database11.zip

    here is my sample database

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your table is not normalized. The date and emailed fields should be in a related table, with a record for each combination. That makes your query simpler.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    @pbaldy - I know that it is not normalized. I set this sample db up as my normal db is set up right now. I am actually in the process of changing that over to the proper normalization that should be used. It was not set up properly before. I was just curious if there was a way to accomplish my goal with the way it is now so I would have this function available for use now and not have to wait until I am done normalizing it.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A UNION query is common way of pulling non-normalized data into a normalized format that can be worked with more easily.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    I haven't spent much time at all working with UNION queries. I am just going to proceed with fixing the db to work more efficiently. Thanks for the advice though.

    Thanks for all of your help. It is working exactly how I want it to right now.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-26-2012, 09:37 AM
  2. Replies: 2
    Last Post: 04-27-2012, 10:48 AM
  3. Replies: 3
    Last Post: 03-08-2012, 04:43 PM
  4. Automatically Send E-Mail from Access
    By lilygtg in forum Access
    Replies: 1
    Last Post: 08-24-2011, 05:48 PM
  5. Emails from Access
    By dbn00bz in forum Access
    Replies: 0
    Last Post: 12-07-2009, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums