VBA code to email report from command click on form

JRUD

New member
Local time
Today, 07:35
Joined
Feb 21, 2017
Messages
6
hi,

I have created a form that loads as add new record.
I also have a command button on my form that on click prints the current record entered as a report.

I would like the printed report to be sent in an email on the same command.

can anyone help with the code for this?

This is what I have so far on the command button

Private Sub cmdPrint_Click()
If Me.Dirty Then

Me.Dirty = False

End If

'Print current record
'using rptQuery.
If IsNull(Me!ID) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "rptQuery", , , _
"ID = " & Me!ID
DoCmd.Close acForm, "Goods In Delivery Query Form"

End Sub

TIA
 
DoCmd.SendObject acSendReport, "rMyReport", acFormatPDF, "name@aol.com", , , "Subject", "message"
 
Thanks RRanman256

what position should I add this at on my existing code?
 
I have managed to use the code

DoCmd.SendObject acSendReport, "rptName", acFormatPDF, "emailaddress.com", , , "subject", "Message "

however this is emailing all records from the report, I would like to only send the current record entered in the form. I'm thinking I need a where condition for my ID field, maybe similar to the one used to print the report.

can anyone help what expression I need and where it needs to go?

much appreciated

James
 
Simply open the report filtered, before using the send object command. Access will use already opened filtered report as the output. So something like

Code:
DoCmd.OpenReport "rptQuery", , , "ID = " & Me!ID
DoCmd.SendObject acSendReport, "rptQuery", acFormatPDF, "emailaddress.com", , , "subject", "Message "
DoCmd.Close "rptQuery"
 
Simply open the report filtered, before using the send object command. Access will use already opened filtered report as the output. So something like

Code:
DoCmd.OpenReport "rptQuery", , , "ID = " & Me!ID
DoCmd.SendObject acSendReport, "rptQuery", acFormatPDF, "emailaddress.com", , , "subject", "Message "
DoCmd.Close "rptQuery"

Thanks for the reply Minty,

I am new to all this so sorry if i'm asking silly questions. I have got a little confused with the code you've gave me. where exactly should it go? and will I need to edit some properties on my report for it to work?

this is what I now have on my command button on my form

Private Sub cmdPrint_Click()
If Me.Dirty Then
Me.Dirty = False
End If
'Print current record
'using rptQuery.
If IsNull(Me!ID) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "rptQuery", , , _
"ID = " & Me!ID
DoCmd.OpenReport "rptQuery", , , "ID = " & Me!ID
DoCmd.SendObject acSendReport, "rptQuery", acFormatPDF, "jamesemailhotmail.com", , , "subject", "message"
DoCmd.Close "rptQuery"
DoCmd.Close acForm, "entry form"
End Sub

this is still giving me all my report results in the email

TIA
 

Users who are viewing this thread

Back
Top Bottom