Solved

SendObject  only as an attachment-need it in email body

Posted on 2013-02-07
5
287 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now