473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Email Report using Send Object and recordset

I have a command button on a form that opens an (outlook) email with a correlated report attached. All works well, except for the cc address. In most instances there is no cc designate to send to, so most of the records in the record set are empty - which causes an error. I have tried so many, many different things to try to get my code to ignore empty fields. Any suggestions. The code follows:
Private Sub btnSendEmailReport_click()
On Error GoTo Err_btnSendEmailReport_Click

Dim MyBody As TextStream
Dim Rst As DAO.Recordset
Dim AppOutlook As Outlook.Application
Dim MailOutlook As Outlook.MailItem
Dim StrDocname As String
Dim StrEmail As String

Set AppOutlook = CreateObject("Outlook.Application")
Set MailOutlook = AppOutlook.CreateItem(olMailItem)

Set Rst = CurrentDb.OpenRecordset("SELECT DISTINCT tblBudgetsFaculty.BudgetsFacultyEmployee_Number, tblBudgetsFaculty.BudgetsFacultyMonthlyEmail, tblBudgetsFaculty.BudgetsFacultyMonthlycc, tblBudgetsFaculty.BudgetsFacultyPreferredName, tblBudgetsFaculty.BudgetsFacultyAccountName, tblBudgetsFaculty.BudgetsFacultyMonthlyRep, tblBudgetsFaculty.BudgetsFacultyFY From tblBudgetsFaculty WHERE (((tblBudgetsFaculty.BudgetsFacultyFY)= '17') OR ((tblBudgetsFaculty.BudgetsFacultyFY) = '00')) AND ((tblBudgetsFaculty.BudgetsFacultyMonthlyRep)='yes ')")

StrDocname = "rptFacultyOrderHistoryForEmail"

Rst.MoveFirst
Do While Rst.EOF = False

If (Rst!BudgetsFacultyMonthlyRep) <> "Yes" Then
GoTo skip_email
End If

DoEvents
DoCmd.OpenReport StrDocname, acViewPreview, , "BudgetsFacultyEmployee_Number = " & Rst!BudgetsFacultyEmployee_Number, acHidden
DoCmd.SendObject acSendReport, StrDocname, acFormatPDF, Rst!BudgetsFacultyMonthlyEmail, Rst!BudgetsFacultyMonthlycc, , _
"Budget Report for: " & Rst!BudgetsFacultyAccountName & " as of " & Date, _
"Dear " & Rst!BudgetsFacultyPreferredName & ":" & vbNewLine & vbNewLine & _
"Attached is a budget report for the account above." & vbNewLine & vbNewLine & _
"If you have any questions, please let me know." & vbNewLine & vbNewLine & _
"Best-" & vbNewLine & _
"Colleen", True
DoCmd.Close acReport, StrDocname, acSaveNo

skip_email:
Rst.MoveNext

Loop
Rst.Close
Set Rst = Nothing

Exit_btnSendEmailReport_click:
Exit Sub

Err_btnSendEmailReport_Click:
MsgBox Err.Description
Resume Exit_btnSendEmailReport_click
End Sub
Oct 20 '16 #1

✓ answered by jforbes

You might try using Nz() around the BudgetsFacultyMonthlycc field, kind of like this:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SendObject acSendReport, StrDocname, acFormatPDF, Rst!BudgetsFacultyMonthlyEmail, Nz(Rst!BudgetsFacultyMonthlycc, ""), , _
  2.  "Budget Report for: " & Rst!BudgetsFacultyAccountName & " as of " & Date, _
  3.  "Dear " & Rst!BudgetsFacultyPreferredName & ":" & vbNewLine & vbNewLine & _
  4.  "Attached is a budget report for the account above." & vbNewLine & vbNewLine & 
When using OpenRecordset, if a field is stored as a Null it will resolve to a Null when using Rst!BudgetsFacultyMonthlycc all by itself. Most functions will not take a Null as a parameter. By using an Nz(), you can pass an Empty String instead of a Null. This post explains it better: What is the difference between "" and Null?

This is just a guess of what is going on. If this doesn't fix it, then let us know the error you are experiencing and on what line of code you are experiencing it on.

3 1255
jforbes
1,107 Expert 1GB
You might try using Nz() around the BudgetsFacultyMonthlycc field, kind of like this:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SendObject acSendReport, StrDocname, acFormatPDF, Rst!BudgetsFacultyMonthlyEmail, Nz(Rst!BudgetsFacultyMonthlycc, ""), , _
  2.  "Budget Report for: " & Rst!BudgetsFacultyAccountName & " as of " & Date, _
  3.  "Dear " & Rst!BudgetsFacultyPreferredName & ":" & vbNewLine & vbNewLine & _
  4.  "Attached is a budget report for the account above." & vbNewLine & vbNewLine & 
When using OpenRecordset, if a field is stored as a Null it will resolve to a Null when using Rst!BudgetsFacultyMonthlycc all by itself. Most functions will not take a Null as a parameter. By using an Nz(), you can pass an Empty String instead of a Null. This post explains it better: What is the difference between "" and Null?

This is just a guess of what is going on. If this doesn't fix it, then let us know the error you are experiencing and on what line of code you are experiencing it on.
Oct 21 '16 #2
Oh my ... I swear I did exactly what you suggested using the Nz function! I just tested it and it works just dandy. I am totally exasperated ... I've been working on this for a week now. <sigh>

Sorry for the intrusion. Have a great weekend!
Oct 21 '16 #3
jforbes
1,107 Expert 1GB
Getting a good conceptual grasp on Nulls and Empty Strings can be exasperating. Glad you got things working.
Oct 21 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ashishthaps | last post by:
How to track the status of the email sent using outlook object in vb.net. The status can be one of the following 1. Sent successfully. 2. Failed. 3. Any other status.
7
by: Nothing | last post by:
I have a report based on a query that is based agaisnt two tables. The report pulls information for 1 customer. The tables may hold multipul customers. I want to email just the ONE report. All...
0
by: Nanker | last post by:
I would like to integrate Reporting Services reports into an existing email messaging framework using .NET 1.1 (both to leverage the existing framework and since the error reporting by Reporting...
46
by: pixie | last post by:
Hi. I have an Access DB that is for contracts. I have used the code found on Tony's site for emailing a report per person containing only their information using GroupWise. It works great but I...
1
by: Peter Herath | last post by:
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want...
1
by: Ramchandar | last post by:
I would like to create a dynamic report in excel using VB6.0. I am having many recordset and i would be using these separate recordset to prepare report in excel. I have a separate format also for...
1
by: ravibhartiya | last post by:
Hi, I am using the following code for sending email Sub SendEmail(iOrgId,strEmailFrom,strEmailSubject,strEmailBody,strEmailTo) Dim ObjEmail ObjEmail.To=strEmailTo ObjEmail.From=strEmailFrom...
68
by: Jeffrey Tan | last post by:
Hi. I was wondering if there was a way to send an email without using the Send Object method for a macro. if you use the macro, you will get this warning:...
1
by: Maraj | last post by:
I have a problem tracking emails.I want to know who read my email.When i send mail to my outlook it works fine but when i want to send it across the network it does not work.ie. When i debug my code...
44
by: aflores41 | last post by:
http://bytes.com/topic/access/answers/891050-send-email-based-query Best example is from the link above however, I need it to be modified. Basically, I have a query with many fields. 3 fields...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.