Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    Hi June7

    Thank you for the ongoing advice. Yes my naming conventions were a big mistake, which I did not realise at the time!!



    I am having an problem with the Set rsData line. could you advise please?

    Code:
    Public Function SendTestEMail()
    Dim rsData As Recordset
    Dim rsEmails As Recordset
    Dim strBody As String
    Set rsEmails = CurrentDb.OpenRecordset("SELECT eMail FROM TStaffList;")
    While Not rsEmails.EOF
        Set rsData = CurrentDb.OpenRecordset("SELECT * FROM TStaffList WHERE TStaffList=" & rsEmails!TStaffList & ";")
        While Not rsData.EOF
            'code to build email body using rsData record
            strBody = strBody & rsData!field1 & ", " & rsData!Field2 & ", " & rsData!field3 & vbCrLf
            rsData.MoveNext
        Wend
        'code to send email
        rsData.Close
        rsEmails.MoveNext
    Wend
    End Function

  2. #17
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    rsData needs to reference the table that has the data you want to send in the email body. What data do you want to send in this email? You want each email to have only the data that is relevant to each recipient? Both tables must have some value to filter the records for the specific recipient, like a StaffID.

    ("SELECT eMail, StaffID FROM TStaffList;")

    ("SELECT * FROM [the data table name] WHERE StaffID=" & rsEmails!StaffID)

    Then construct the body of the email with the fields from rsData. Use your field names instead Field1, Field2, Field3.

    You don't show the code that actually constructs and sends email. Refer back to post 2 for that.

    An alternative to opening the rsData recordset and building email is the OutputTo method which will attach a PDF of a report object to an email.
    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. #18
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    Can you advise me on the code needed to use the OutputTo method to loop through the e-mail address's and attach a PDF with the relevant data and send to that e-mail address.

    Thanks for your continued help... I know, I am very frustrating...

  4. #19
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Sorry, I meant SendObject.

    Like:
    Code:
    Public Sub SendTestEMail()
    Dim rsEmails As Recordset
    Set rsEmails = CurrentDb.OpenRecordset("SELECT eMail, StaffID FROM TStaffList;")
    While Not rsEmails.EOF
        DoCmd.OpenReport "report name", , , "StaffID=" & rs!StaffID
        DoCmd.SendObject acSendReport, , acFormatPDF, rs!eMail, , , "subject", "message", False
        DoCmd.Close acRecport, "report name", acSaveNo
        rsEmails.MoveNext
    Wend
    End Sub
    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. #20
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    Hi June7, I have spent some time looking at the Do.Cmd items below and familiarised myself with them. I am still having some issues.

    The
    Code:
     DoCmd.OpenReport "RTestReport", , , "StaffID=" & rs!StaffID
    does not run...

    Is this because I have not set a rsStaffID?

    Code:
    Public Function SendTestEmail()
    
    Dim rsEmails As Recordset
    Set rsEmails = CurrentDb.OpenRecordset("SELECT eMail, StaffID FROM TStaffList;")
    While Not rsEmails.EOF
        DoCmd.OpenReport "RTestReport", , , "StaffID=" & rs!StaffID
        DoCmd.SendObject acSendReport, , acFormatHTML, rs!Email, , , "Test Report", "message", False
        DoCmd.Close acRecport, "RTestReport", acSaveNo
        rsEmails.MoveNext
    Wend
    End Function

  6. #21
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    The recordset is declared and set.

    I don't know your data structure. I don't know your table and field names. Substitute into the code as appropriate for your db. Do you have a field named StaffID in TStaffList?

    My suggested procedure is a Sub, not a Function.

    The Sub could be a button Click event.
    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.

  7. #22
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    I have 2 tables. One table is TStaffList that includes the fields; StaffID, ForeName, SurName, eMail. The other table is the TCorrectionLog which has fields: StaffID Date, Time, Error. The two StaffID fields are related and the TStaffList acts as a look up for TCorrectionLog.

    I need to loop through the StaffId's in the StaffID field in TStaffList and use each StaffID as a parameter in a query to extract only the records with same StaffID from the TCorrectionLog.

    This is more than likely how you imagined it but just to give you info on the structure of the DB

  8. #23
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Then the code looks appropriate. But why is it a Function? What event are you calling it from? Why not just put the code in a button Click event?

    Refer to link at bottom of my post for debugging guidelines.
    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.

  9. #24
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    Hi June7

    I have changed from a function to a sub as advised.

    My current code still not working

    Code:
    Private Sub Command0_Click()
    Dim rsEmails As Recordset
    Set rsEmails = CurrentDb.OpenRecordset("SELECT eMail, StaffID FROM TStaffList;")
    While Not rsEmails.EOF
        DoCmd.OpenReport "testReport", , , "StaffID=" & rs!StaffID
        DoCmd.SendObject acSendReport, , acFormatHTML, rs!Email, , , "subject", "message", False
        DoCmd.Close acRecport, "testReport", acSaveNo
        rsEmails.MoveNext
    Wend
    End Sub
    I tried to understand the debugging procedures with not much luck.

    The line
    Code:
     DoCmd.OpenReport "testReport", , , "StaffID=" & rs!StaffID
    is the one showing as yellow.

    Can you advise me on how to fix the code to have it run effectively.

    Thanks again!!

  10. #25
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    What is the exact error message?

    Is StaffID a number or text field?

    If it is text the parameter must have apostrophe delimiters.

    DoCmd.OpenReport "testReport", , , "StaffID='" & rs!StaffID & "'"

    Date data type would use # delimiter.
    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.

  11. #26
    craig1988 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2014
    Posts
    82
    StaffID is a text field so I have added the delimiter as advised.

    The same line is still yellow though

    Code:
     DoCmd.OpenReport "RMaster", , , "StaffID'=" & rs!StaffID & "'"
    When I click the button the Dialog box pops up with "Run time error '424': Object required"

    When I click debug and hover over rs!StaffID a popover appears saying "rs!StaffID = <Object required>

  12. #27
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    You put the first delimiter in the wrong place. It goes after the = sign. See my example.
    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.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 08-14-2014, 06:00 AM
  2. Replies: 2
    Last Post: 06-17-2014, 02:18 PM
  3. Replies: 1
    Last Post: 04-14-2014, 12:29 PM
  4. Replies: 5
    Last Post: 08-24-2013, 02:07 AM
  5. Replies: 7
    Last Post: 10-30-2012, 02:18 PM

Tags for this Thread

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