Results 1 to 14 of 14
  1. #1
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6

    Send email from access with the data includes in the record set

    Hi All,
    i need to send by outlook mail the content of a record in the database access. i have tried this code

    Private Sub Comando29_Click()

    ' https://youtu.be/6fu0kXXU_R4 - MANDARE EMAIL CON ACCESS


    Dim OutApp As Object
    Dim OutMail As Object
    Dim strMsg As String
    Dim strDest As String
    Dim strAllegato As String


    Set OutApp = CreateObject("outlook.application")
    Set OutMail = OutApp.CreateItem(0)


    strMsg = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>"
    strMsg = strMsg & "<html>"
    strMsg = strMsg & "<head>"
    strMsg = strMsg & " <meta content='text/html; charset=ISO-8859-1'"
    strMsg = strMsg & " http-equiv='content-type'>"
    strMsg = strMsg & " <title></title>"
    strMsg = strMsg & "</head>"
    strMsg = strMsg & "<body>"
    strMsg = strMsg & "<span style='font-weight: bold;'>Spett.</span><br>"
    strMsg = strMsg & "<br>"


    strMsg = strMsg & "i dati sono:<br>"
    strMsg = strMsg & "<br>"
    strMsg = strMsg & "<table style='text-align: left; width: 100%;' border='1'"
    strMsg = strMsg & " cellpadding='2' cellspacing='2'>"
    strMsg = strMsg & " <tbody>"
    strMsg = strMsg & " <tr>"
    strMsg = strMsg & " <td>ffff</td>"
    strMsg = strMsg & " <td>fff</td>"
    strMsg = strMsg & " <td>fff</td>"
    strMsg = strMsg & " <td>fff</td>"
    strMsg = strMsg & " </tr>"
    strMsg = strMsg & " <tr>"
    strMsg = strMsg & " <td><p align='center'><strong>" & Format(DESCRIZIONE_ELABORATO, "Standard") & "</strong></p> </td>"


    ' Ottieni il percorso dell'allegato
    strAllegato = CurrentDb.OpenRecordset("02_TAB_REV").Fields("Prog ettazione").Value


    ' Aggiungi l'allegato alla email
    OutMail.Attachments.Add FileName:=strAllegato


    strMsg = strMsg & "</tbody>"
    strMsg = strMsg & "</table>"
    strMsg = strMsg & "<br>"
    strMsg = strMsg & "<br>"
    strMsg = strMsg & "saluti,<br>"
    strMsg = strMsg & "</body>"
    strMsg = strMsg & "</html>"
    strDest = txtEmail ' variabile email


    With OutMail
    .to = strDest
    .cc = ""
    .bcc = ""
    .subject = "scad pag"

    .HTMLBody = strMsg
    .Display
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing


    End Sub

    but it gives me an error.... why?
    could you help me?

    thx

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You need to add the fields of the recordset into the body of your email.
    Attachments are literally that .....Attachments. You would use these to attach a file like a report.
    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

  4. #4
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6
    Quote Originally Posted by orange View Post
    What is the error number and message?
    runtime 13

    line error:

    strAllegato = CurrentDb.OpenRecordset("02_TAB_REV").Fields("Prog ettazione").Value

  5. #5
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6
    Quote Originally Posted by Welshgasman View Post
    You need to add the fields of the recordset into the body of your email.
    Attachments are literally that .....Attachments. You would use these to attach a file like a report.

    i want to attach file loaded in the db access, in a recordset of access, not an external one or send a report. :..(

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I don't think you can open a recordset and get a field value in the same line? AFAIK, you have to open first then refer to the rs field. With that you will get one value, which seems odd to build an html table for that. BTW, your table code is missing an end tag for the 2nd table row (at least I can't see it).

    Please use code tags (# icon on posting toolbar) for code, not comment tags, and maintain indentation in your code. Also, when asked for the error message please provide it or at least a close proximity. There are thousands of error numbers and we shouldn't have to look them up. You'd think that with any experience these should be memorized, but it is the opposite. The more you practice the less you see these errors so the meaning of the numbers is easily forgotten.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6
    Quote Originally Posted by Micron View Post
    I don't think you can open a recordset and get a field value in the same line? AFAIK, you have to open first then refer to the rs field. With that you will get one value, which seems odd to build an html table for that. BTW, your table code is missing an end tag for the 2nd table row (at least I can't see it).

    Please use code tags (# icon on posting toolbar) for code, not comment tags, and maintain indentation in your code. Also, when asked for the error message please provide it or at least a close proximity. There are thousands of error numbers and we shouldn't have to look them up. You'd think that with any experience these should be memorized, but it is the opposite. The more you practice the less you see these errors so the meaning of the numbers is easily forgotten.

    ok sorry fow #..... could you show me the code to do what you say?
    thanks...

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Open a recordset object:

    Dim rs as DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("02_TAB_REV")


    Refer to fields of recordset wherever needed - with concatenation to combine with static strings:

    strMsg = strMsg & " <td>" & rs("Prog ettazione") & "</td>"


    What do you mean by "file loaded in the db access"? If you mean a file like Excel spreadsheet, Word doc, or image saved in Attachment field in table - the above won't work. The file must first be saved out to folder location then pulled in to the Attachment property of email by referencing that folder/file path. Review https://learn.microsoft.com/en-us/of...ile-method-dao


    Also, strongly advise not to use spaces in naming convention.
    Last edited by June7; 12-19-2023 at 01:04 PM.
    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. #9
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6
    ok... so i can't send attach file in db directly from db.... so how i can save in the same folder the file and then take it to send by emal and delete it?
    i think the procedure can be only:

    1- click on a button to save file in the same directory of Access DB
    2- take it or them from the folder and attach them in a mail

    could help me to do this?

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Need to get your terminolgy correct.
    As it stands, you are trying to email the value of one field?
    That can easily be done as @June7 has shown.

    So no real need for an attachment in this case.
    Plenty of examples on the net on how to attach files to an outlook email on the net.
    If it was a set of records in a query, investigate SendObject command.
    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
    Brilli is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2023
    Posts
    6
    i want to take the file into access db record.
    i have tried this example but i have had run time error. i try again.

    thx

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    i want to take the file into access db record.
    I do not understand what you are saying?
    Tell us what the error is, show us your latest code (within code tags)

    It is called a language for a reason. You cannot just make up words yourself.
    There is a structure to the language and the objects.
    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

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    As you say in post #9 here are the steps you need to take:

    1.Save the attachment to local folder (I recommend using the same folder the front-end is in, you get that by using CurrentProject.Path). Here is a link to accomplish this step:https://learn.microsoft.com/en-us/of...ile-method-dao
    2.Modify your emailing code to use the Attachments collection of the email message object to add the newly downloaded file. Here is a link to help you with this step:https://stackoverflow.com/questions/...-as-attachment
    3. Delete the newly created file by using the Kill command: https://stackoverflow.com/questions/...-a-file-in-vba

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Did you even look at the link referenced in post 8?
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2019, 03:02 PM
  2. Replies: 4
    Last Post: 07-12-2018, 05:38 AM
  3. Replies: 3
    Last Post: 09-30-2016, 04:49 PM
  4. Replies: 1
    Last Post: 09-07-2014, 10:15 PM
  5. Replies: 9
    Last Post: 10-09-2009, 08:15 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