Add Query Results to Body of Email

aldeb

Registered User.
Local time
Today, 05:24
Joined
Dec 23, 2004
Messages
318
Can anyone tell me how to put the results of a query in the body of an email in Access? Can It be done with a DoCmd?
 
How will DoCmd.SendObject get query results in the BODY of my email?
 
Does it not default to putting the query results into the body of the email? Would be silly if it stuck it in the header!
I have never done this, it was just a suggestion
 
Does it not default to putting the query results into the body of the email?

Well, No it doesn't. The full syntax is...

DoCmd.SendObject [objecttype],[objectname],[outputformat],[to],[cc],[bcc],[subject],[ messagetext],[editmessage],[templatefile]

If you were to use this.....
DoCmd.SendObject acQuery, "NameOfQuery", , "Someone@someplace.com", , , , , False, ""

It would prompt you for an output format and send it as an attachment. Which depending on the size of your query, an attachment probably would be best...... A large query might get rather difficult to read in the body of an email.... I would send it as an Excel. This would enable it to be imported or linked to another database by the recipient. Either wait for the prompt, or add the format as so.....
DoCmd.SendObject acQuery, "NameOfQuery", "(*.xls)", "Someone@someplace.com", , , , , False, ""
 
Can I assume you are going to be using Microsoft Outlook? If so, you can use VBA to use the Outlook Object to create an email. If you are here is a snippet of the coding: (Make sure you add a reference in Access to the Microsoft Outlook Object Library for this to work)

Dim strHtml 'a variable to store the results of your query in a string
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olAtt As Outlook.Attachments

Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
Set olMail = olFolder.Items.Add("IPM.note")
Set olAtt = olMail.Attachments

With olMail
.Subject = [Your Subject Here]
.To = emailAddress
.HTMLBody = strHtml ' use .body for regular text
.send
End With


Of course you would have to build a string by looping through the recordset of the query. This can be done easily - even to build an HTML table that would display the data in a table.

One last thing: If you really are willing to play around, you could download and add in a reference to the Redemption Object (http://www.dimastr.com/redemption/) which would allow you to have Access send the email without Outlook prompting you with a security message every time you try to email.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom