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

    Email to Current Record Only


    I have a change of address form with this code to open a report for the current record.
    Code:
    Dim strWhere As String
    If Me.Dirty Then 'Save any edits
    Me.Dirty = False
    End If
    If Me.NewRecord Then 'Check these is a record to print
    MsgBox "Select a record to print"
    Else
    strWhere = "ID = " & Me.ID
    DoCmd.OpenReport "ClientOldAddressLetter", acViewPreview, , strWhere
    End If
    I have a button that I want to have send an email to a specified person regarding the change. I have the below code to send an email and my question is how do I set it to just send for the current record only, just like the report opens for?
    Code:
    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 = "ClientChangeOfAddressQRY"
            Set dbs = CurrentDb
            Set rst = dbs.OpenRecordset(strSql)
            rst.MoveFirst
            While Not rst.EOF
                recipient = "example@example.com"
                Set myOlApp = CreateObject("Outlook.Application")
                Set myItem = myOlApp.CreateItem(olMailItem)
                Set myAttachment = myItem.Attachments
                Set myRecipient = myItem.Recipients.Add(recipient)
                myItem.CC = "example@example.com"
                myItem.Subject = "Example"
                myItem.Body = "Example"
                myItem.Display
                rst.MoveNext
            Wend
            
        'DoCmd.Close acForm, "ClientChangeOfAddress"
        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
    Can I put the report code in here some here? I can't seem to get it.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    With that method, would have to save the report as an image (probably PDF) file, then attach the file to the email.

    DoCmd.SendObject method will automatically convert the report to PDF and attach.

    Is the email address in the record on form?

    recipient = Me!fieldname

    Need to comment or remove code that is not needed, such as the myItem.CC
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    No. The email is in a query where I bring in the specific rep for the client whose address has changed.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    If you want to send only one email based on form current record, then don't loop through a recordset, which is what the sample code is designed to do.

    How will the procedure get the email address if it's not in the form record? Do you want to do a DLookup in the code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    214
    Thanks for the DLookup suggestion, I didn't think of that. So far it is working as I want.

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

Similar Threads

  1. Email a report of the current record.
    By jonudden in forum Reports
    Replies: 4
    Last Post: 08-21-2023, 07:37 AM
  2. Replies: 16
    Last Post: 02-06-2013, 03:06 PM
  3. Replies: 13
    Last Post: 11-07-2012, 03:14 PM
  4. Replies: 3
    Last Post: 09-19-2012, 07:34 AM
  5. Replies: 5
    Last Post: 08-24-2012, 10:32 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