Solved

SendObject  only as an attachment-need it in email body

Posted on 2013-02-07
5
291 Views
Last Modified: 2013-03-22
I am using Access 2003 and Outlook 2010 (Cloud).  I am trying to use SendObject to write Query data results into the body of an email - not as an attachment.

Is this possible?

Thanks!
Nancy
0
Comment
Question by:NEC293
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 38864998
It should be possible.  You'll need to construct the message body as a string based upon the query result set.  Something like this

Dim rs As DAO.Recordset
Dim strBody As String

Set rs = CurrentDb.OpenRecordset ("MyQuery")

strBody = "Good Day.  Here's the daily report from the database:  " & vbCrLf & vbCrLf

rs.MoveFirst
Do Until rs.EOF
    strBody = strBody & rs(0) & ", " & rs(1) & ", " & rs(2) & ", " & etc. etc. & vbCrLf
    rs.MoveNext
Loop

strBody = strBody & vbCrLf & "That's it!"

    'send the report as an email msg
    'DoCmd.SendObject acSendNoObject is default, ObjectName, OutputFormat, To, CC, BCC, Subject, Body
DoCmd.SendObject , , , "TheRecipient@MyDomain.com", "ccAddress@Mydomain.com", , "Daily Report", strBody

    'destroy object variable
Set rs = Nothing



OM Gang
0
 

Author Comment

by:NEC293
ID: 38873562
I'm rather a novice at this ... the query I am running uses a date range entered by the user.
Does that make a difference in using this code?
0
 
LVL 28

Expert Comment

by:omgang
ID: 38876130
Yes.  You need to explicitly declare the parameters for the parameter query when opening a DAO recordset upon it.

How are the users entering the date range?  Prompt from the query itself or via fields/controls on a form?

OM Gang
0
 

Author Comment

by:NEC293
ID: 38877224
Prompt from the query itself.
0
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 38877552
OK.  I'll work off the assumption your query has a criteria expression in it something like this
WHERE (((data.dDate) Between [Enter Start] And [Enter End]));

The bracketed values [Enter Start] & [Enter End] are called parameters.  We need to explicitly supply those when opening a recordset on the query object

So we make the following changes

Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim dStart As Date, dEnd As Date
Dim strBody As String

    'here we prompt the user to inpute the start and end date values.
dStart = CDate(InputBox("Enter start date"))
dEnd = CDate(InputBox("Enter end date"))

Set qdf = CurrentDb.QueryDefs("MyQuery")
qdf.Parameters("[Enter Start]" = dStart
qdf.Parameters("Enter End]") = dEnd
Set rs = qdf.OpenRecordset ("MyQuery")

strBody = "Good Day.  Here's the daily report from the database:  " & vbCrLf & vbCrLf

rs.MoveFirst
Do Until rs.EOF
    strBody = strBody & rs(0) & ", " & rs(1) & ", " & rs(2) & ", " & etc. etc. & vbCrLf
    rs.MoveNext
Loop

strBody = strBody & vbCrLf & "That's it!"

    'send the report as an email msg
    'DoCmd.SendObject acSendNoObject is default, ObjectName, OutputFormat, To, CC, BCC, Subject, Body
DoCmd.SendObject , , , "TheRecipient@MyDomain.com", "ccAddress@Mydomain.com", , "Daily Report", strBody

    'destroy object variables
Set rs = Nothing
Set qdf = Nothing



OM Gang
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now