Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131

    Send Email

    We currently use Access to send emails that have information and query's or reports attached to them. I would like to use the text of a query and put it in the subject or message box of the email instead of sending a report or query. Can this be done?

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    I think you can use Docmd.SendObject and just omit the optional parameters(lookup DoCmd.SendObject online)? Maybe like:

    DoCmd.SendObject ,, ,"SendTo", , , Me.YourSubject, Me.YourBodyMessage, , False

    Also if you go to Forum, Code Repository on this site, you should be able to find some email code examples.


  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What do you mean by the 'text of a query' - data from fields? For only one record? Code can concatenate data with literal text to construct a string for use as email subject and/or body. If a form is open to the desired record, it is simple for code behind form to reference bound controls. As Bulzie said, common topic - search forum and Google.
    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.

  4. #4
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    This is my current code;

    If Me.Dirty Then Me.Dirty = False
    LResponse = MsgBox("An email will be sent to operations of a termination.", vbYes, "Employee Termination Notice")
    DoCmd.SendObject acQuery, "qry_EmployeeTerminationNotice", "Excel97-Excel2003Workbook(*.xls)", "timt@gmail.com", "", "", "Termination Notice", "", True, ""

    Instead of the users having to open up an Excel file, I would like the information in the query (which is (John Smith has been terminated on 6/1/2017) in the message portion of the email.

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    You could do a DLookup maybe to get that statement.

    vTermText = DLookup("[YourTextField]", "qry_EmployeeTerminationNotice")

    DoCmd.SendObject acQuery, "qry_EmployeeTerminationNotice", "Excel97-Excel2003Workbook(*.xls)", "timt@gmail.com", "", "", "Termination Notice", vTermText, True, ""


  6. #6
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    Thank you, it looks very promising. I am out of the office today but will be back on Monday to try the code. Will it also send the file?

  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Maybe something like below. You might have to test it and revise as needed or others my spot if something needs to be changed.

    vTermText = DLookup("[YourTextField]", "qry_EmployeeTerminationNotice")

    DoCmd.SendObject acSendQuery, "qry_EmployeeTerminationNotice", acFormatXLS, "timt@gmail.com", "", "", "Termination Notice", vTermText, True, ""

  8. #8
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    This worked freat for the project that just had one line of code in it, now they want several (75 or so) to be sent with the email. When using the above code, I get the first line item, that is it.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Options:

    1. SendObject sends query or report as PDF attachment

    2. VBA opens recordset object, loops recordset to concatenate a very long string of data as the body text for SendObject
    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.

  10. #10
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Loop through your query to send all the emails. See the change in bold where you get the address from the query as move through each record. Syntax might not all be right in that code but that's the idea.

    Dim db as database, rs1 as Recordset
    Set db = Currentdb
    Set rs1 = db.OpenRecordset("qry_EmployeeTerminationNotice")

    rs1.movefirst

    Do Until rs1.eof

    vTermText = DLookup("[YourTextField]", "qry_EmployeeTerminationNotice")
    DoCmd.SendObject acSendQuery, "qry_EmployeeTerminationNotice", acFormatXLS, rs1!EmailAddress", "", "", "Termination Notice", vTermText, True, ""

    rs1.moveNext
    Loop

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    If you need a number of records combined for one email, the message body concatenation would be within a loop but the SendObject would be outside that loop.

    Do you want to send just one email or do you want to send a batch of emails to multiple addresses? And each email has data from multiple records? You might need multiple queries and multiple loops to accomplish what you want. Just need a better understanding of what that is.
    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.

  12. #12
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    Thanks again. I just need to send about 25 - 75 different items to one or two email address.

  13. #13
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    When I get to this line item I get a Run-Time Error 13
    Type mismatch.

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    When you get to what line item? With what 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.

  15. #15
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    Set rs1 = db.OpenRecordset("qry_EmployeeTerminationNotice")

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

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2015, 04:11 PM
  2. Replies: 5
    Last Post: 09-14-2015, 07:24 AM
  3. Send email using CDO, get email addresses from table
    By marvinac1 in forum Programming
    Replies: 3
    Last Post: 12-22-2014, 12:54 PM
  4. Replies: 5
    Last Post: 05-07-2014, 09:25 AM
  5. Replies: 4
    Last Post: 04-13-2011, 10:11 AM

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