Results 1 to 11 of 11
  1. #1
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Email vba problems

    Hi everyone!



    I am trying to email statements to members. One statement per member. Right now Access is putting all of them into one report so members are getting other people's statements as well. Can anyone see what I've missed? I've been through it 100 times at least and cannot figure it out for the life of me. I thank you in advance.

    Code:
    Private Sub Command103_Click()Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
    Dim fileName As String, todayDate As String
    Dim rst As DAO.Recordset
    Dim LastName As Variant
    Dim strBody As String, lngCount As Long, lngRSCount As Long
    Dim strTo As String
    
    
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM [emailinvoicelist]ORDER BY [lastname];")
    
    
    Do While Not rst.EOF
        strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]
        Debug.Print rst![MemberID_PK]
        
        DoCmd.OutputTo acOutputReport, "EmailALLDamAssessStatementG1", acFormatPDF, "C:\Emailtests" & "\" & rst![MemberID_PK] & "dam.pdf"
        DoEvents
    
    
    
    
    Set oEmail = oApp.CreateItem(olMailItem)
    With oEmail
          
          strTo = rst!InvoiceEmail
          fileName = "C:\Emailtests" & "\" & rst![MemberID_PK] & "dam.pdf"
         
          
        Debug.Print rst!InvoiceEmail
        .TO = strTo
        Debug.Print strTo
        Debug.Print fileName
        .Subject = "Statement Attached"
        .Body = "Thank you"
        .Attachments.Add fileName
        .Send
        rst.MoveNext
      
        
    End With
    Loop
    MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,566
    You are not filtering the report for MemberID_PK ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Isn't that what this is?
    Code:
    strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,566
    Quote Originally Posted by Gina Maylone View Post
    Isn't that what this is?
    Code:
    strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]
    Yes, but where do you use it?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    I THINK when it outputs the report it should only output for that member_id.??

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,566
    No, that is why you are getting a complete report for every email.? Even if it did, you still have to use it somewhere, which you have not?

    Try something along the lines of (this is from one of my subs)

    Code:
    Private Sub cmdShip_Click()
    On Error GoTo Err_cmdShip_Click
    
        Dim stRptName As String, stParam As String, stLinkCriteria As String, stDBpath As String, stFTPpath As String
        Dim iPreview As Integer, iDialog As Integer, blnPrintIt As Boolean
        
        stDBpath = CurrentProject.Path & "\"
        stFTPpath = stDBpath & "Gazette\"
        iPreview = acViewPreview
        If Me.ChkPreview Then
           ' iPreview = 2
            iDialog = acWindowNormal
        Else
            iDialog = acHidden
        End If
        
        stRptName = "Main_by_Ship"
        
        stParam = Replace(LCase(Me.cboShip.Value), " ", "_")
        stLinkCriteria = "[Ship] = '" & Me.cboShip.Value & "'"
        
        'DoCmd.CopyObject , stParam, acReport, stRptName
            
        If Me.ChkPreview Then
            DoCmd.OpenReport stRptName, iPreview, , stLinkCriteria, iDialog
        Else
            DoCmd.OpenReport stRptName, iPreview, , stLinkCriteria, iDialog
            DoCmd.OutputTo acOutputReport, stRptName, acFormatPDF, stFTPpath & stParam & ".pdf", False
            DoCmd.Close acReport, stRptName
        End If
        'DoCmd.DeleteObject acReport, stParam
    
    Exit_cmdShip_Click:
        Exit Sub
    
    Err_cmdShip_Click:
        MsgBox Err.Description
        Resume Exit_cmdShip_Click
        
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Thanks Walgasman! I wasn't able to get it to work. What is literally happening is all three (test) members statements are being put in the same PDF. Instead of individual ones. Hope this sheds a little light.

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Gasman showed you the issue.

    You need to open the report filtered, then export it then close it. See below;
    Code:
    
    Private Sub Command103_Click()Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
    Dim fileName As String, todayDate As String
    Dim rst As DAO.Recordset
    Dim LastName As Variant
    Dim strBody As String, lngCount As Long, lngRSCount As Long
    Dim strTo As String
    
    
    
    
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM [emailinvoicelist]ORDER BY [lastname];")
    
    
    
    
    Do While Not rst.EOF
        strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]
        Debug.Print rst![MemberID_PK]
        
    	DoCmd.OpenReport  "EmailALLDamAssessStatementG1", acviewPreview, strRptFilter
        DoCmd.OutputTo acOutputReport, "EmailALLDamAssessStatementG1", acFormatPDF, "C:\Emailtests" & "\" & rst![MemberID_PK] & "dam.pdf"
        DoCmd.Close acReport "EmailALLDamAssessStatementG1"
    	DoEvents
    
    
    
    
    Set oEmail = oApp.CreateItem(olMailItem)
    With oEmail
          
          strTo = rst!InvoiceEmail
          fileName = "C:\Emailtests" & "\" & rst![MemberID_PK] & "dam.pdf"
         
          
        Debug.Print rst!InvoiceEmail
        .TO = strTo
        Debug.Print strTo
        Debug.Print fileName
        .Subject = "Statement Attached"
        .Body = "Thank you"
        .Attachments.Add fileName
        .Send
        rst.MoveNext
      
        
    End With
    Loop
    MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
    End Sub
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Thanks for this Minty,

    This put both reports into the same PDF. So confused.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,566
    I cannot see it would do that?

    Start from basics then.
    Open the report in preview mode and check it is all correct?

    Edit: Ok, it looks like Minty missed a comma for the arguments.
    Look at my syntax, I just copied and pasted my code.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Apologies As WGM spotted I had missed an important "," in the report opening line.
    It should be

    DoCmd.OpenReport "EmailALLDamAssessStatementG1", acviewPreview, , strRptFilter , acWindowNormal
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. Replies: 5
    Last Post: 06-19-2019, 08:57 AM
  2. Replies: 3
    Last Post: 05-15-2017, 03:46 AM
  3. Replies: 5
    Last Post: 05-07-2014, 09:25 AM
  4. Replies: 1
    Last Post: 05-01-2014, 11:37 AM
  5. Email Reports Problems
    By Bamber in forum Reports
    Replies: 1
    Last Post: 05-12-2010, 08:31 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