Solved

SendObject  only as an attachment-need it in email body

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft Office Customization Tool’s Outlook problem 12 86
Excel Hyperlink Question 4 48
How to get many filtered in Excel 2010? 7 31
sumifs excel 2013 3 13
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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…

770 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