How to email from a command button placed on a report ?

M

marc

Hello,


I want to trigger from within a report an email send.

How can I put a command button (or other type of
button) on the report to do a Docmd.sendobject
from the inside of the report ?


Thank you in advance.
M.
 
S

SA

Marc:

There is no way to do this from within a report itself. Graphic object like
buttons don't fire when a report is previewed.

That doesn't mean to say that something similar can not be done, such as
adding toolbar button that would email the report, the current page on the
report or the current group/primary key etc.

1.) To do the easy tool bar button to email the whole report, you can simply
create a function in a general module something like this:

Function SendWholeReport()
On Error resume next
Dim strRptName as String
Dim strRecip
strRptName = Screen.ActiveReport.Name
If len(strRptName) > 0 Then
strRecip = InputBox ("Enter an email Address for this report", "Emailing
Report")
If len(strRecip)>0 Then
docmd.SendObject acReport, strRptName, acFormatRTF, strRecip
End if
End if
End Function

and then create a custom toolbar icon on the report preview tool bar and set
the buttons On Action property to
= SendWholeReport()

2.) To limit what you email by clicking a toolbar command button, to a
specific page or group say and to email the those pages to an email address
that is contained in the report's query or shown on the currently previewed
page, is quite a bit more complex, but not impossible to do . The trick is
that a.) you have to set a filter within the report itself and save it
before emailing otherwise it will send the whole report and b.) that you
have to get and obtain the email that's displayed on the report "out" of the
report into a function that emails the report. So, here's an example of how
you could set up a tool bar button to email only the current group from a
multi-page report that's in preview.

A.) To operate on the open report, i.e. to set a filter on it and to get
data such as an email address that's currently displayed, you must create
public functions and properties for the report that you can call from an
external function and make the report do certain things or get information
from it. In this example a sample report has been created that has a two
field primary key, one of which is a string value and the other a date value
(called respectively MyStringField and MyDateField).

Step 1.) Create in the report's declarations section of the report's module
three variables to "capture" and hold data that is displayed on the specific
report page when it is displayed (i.e. when you go from page to page) Our
example has three values:

'Variables to hold data from the current record
'including its primary key to use for a filter and the
'current displayed email address
Dim strStringKey as String
Dim datDateKey As Date
Dim strEmail As String

Step 2.) In the Report's On Print event for the Detail section (this could
also be an a group header etc.), we capture the values that we'll need based
on what's on the current page. So the code looks like:

Private Sub Detail1_Print(Cancel As Integer, PrintCount As Integer)
On Error Resume Next
strStringKey = Me.MyStringField
datDateKey = Me.MyDateTimeField
strEmail = Me!MyEmailControlName
End Sub

Step 3.) Now that the data is stored in the report in VBA we can use it with
the other functions we need to create for the report which are new public
functions and properties that can be called from an external function. The
first is a function to set the filter for the report so that when SendObject
is called, only the current group in view is output: So add a public
function like this:

Public Function SetFilter()
On Error Resume Next
If Len(strStringKey) > 0 And Not IsEmpty(datDateKey) Then
Me.Filter = "[MyStringField] = '" & strStringKey & "' AND
[MyDateTimeField] = #" & datDateKey & "#"
Me.FilterOn = True
DoCmd.RunCommand acCmdSave
End If
End Function

Step 4.) After the report is output to email, we need to clear the filter to
reset to the "old" full report view and return to the same page. Here's the
code to add to the report's module:

Public Function ClearFilter(intTargetPage)
On Error Resume Next
Me.Filter = ""
Me.FilterOn = False
DoEvents
DoCmd.RunCommand acCmdSave
DoEvents
Me.Page = intTargetPage
End Function

Step 5.) Last we need a method to get the current page of the report we were
on before setting the filter and sending the mail, so we create a public
property that we can call to obtain that:

Public Property Get CurrentPageNum()
CurrentPageNum = Me.Page
End Property

Step 6.) We also need a way to get the email address from the report so that
we can use that when we call send object so add code like this:

Public Property Get CurrentEmail() As String
CurrentEmail = strEmail
End Property

B.) Now that the report has ability to give us the data we need to use in an
external function to send the mail, the easy SendWholeReport function
outlined above is modified to look like this and it is placed in an general
module:

Public Function EMailCurrentRptGroup()
On Error Resume Next
Dim strRptName As String
Dim strEmailAddress As String, intPage As Integer
strRptName = Screen.ActiveReport.Name
If Len(strRptName) > 0 Then
strEmailAddress = Reports(strRptName).CurrentEmail
If Len(strEmailAddress) > 0 Then
intPage = Reports(strRptName).CurrentPageNum 'get the current page
in view
Reports(strRptName).SetFilter 'apply the filter for the current
group
DoEvents
DoCmd.SendObject acSendReport, strRptName, acFormatRTF,
strEmailAddress _
, , , "Here's Your Report", "Attached is an RTF Report", True
DoEvents
End If
Reports(strRptName).ClearFilter (intPage)
End If
End Function

C.) Then create a new custom toolbar button on your preview toolbar in the
the toolbar button's properties for the On Action property specify =
EmailCurrentRptGroup()

That will do it.
--
Steve Arbaugh
ACG Soft
http://ourworld.compuserve.com/homepages/attac-cg
 
M

marc

Thank you so much for your input!


Another thing:
I wish to change a command button's vba script
according to different values (so it is dynamic)

Is there a way to do that ?


Thanks again
M.



SA said:
Marc:

There is no way to do this from within a report itself. Graphic object like
buttons don't fire when a report is previewed.

That doesn't mean to say that something similar can not be done, such as
adding toolbar button that would email the report, the current page on the
report or the current group/primary key etc.

1.) To do the easy tool bar button to email the whole report, you can simply
create a function in a general module something like this:

Function SendWholeReport()
On Error resume next
Dim strRptName as String
Dim strRecip
strRptName = Screen.ActiveReport.Name
If len(strRptName) > 0 Then
strRecip = InputBox ("Enter an email Address for this report", "Emailing
Report")
If len(strRecip)>0 Then
docmd.SendObject acReport, strRptName, acFormatRTF, strRecip
End if
End if
End Function

and then create a custom toolbar icon on the report preview tool bar and set
the buttons On Action property to
= SendWholeReport()

2.) To limit what you email by clicking a toolbar command button, to a
specific page or group say and to email the those pages to an email address
that is contained in the report's query or shown on the currently previewed
page, is quite a bit more complex, but not impossible to do . The trick is
that a.) you have to set a filter within the report itself and save it
before emailing otherwise it will send the whole report and b.) that you
have to get and obtain the email that's displayed on the report "out" of the
report into a function that emails the report. So, here's an example of how
you could set up a tool bar button to email only the current group from a
multi-page report that's in preview.

A.) To operate on the open report, i.e. to set a filter on it and to get
data such as an email address that's currently displayed, you must create
public functions and properties for the report that you can call from an
external function and make the report do certain things or get information
from it. In this example a sample report has been created that has a two
field primary key, one of which is a string value and the other a date value
(called respectively MyStringField and MyDateField).

Step 1.) Create in the report's declarations section of the report's module
three variables to "capture" and hold data that is displayed on the specific
report page when it is displayed (i.e. when you go from page to page) Our
example has three values:

'Variables to hold data from the current record
'including its primary key to use for a filter and the
'current displayed email address
Dim strStringKey as String
Dim datDateKey As Date
Dim strEmail As String

Step 2.) In the Report's On Print event for the Detail section (this could
also be an a group header etc.), we capture the values that we'll need based
on what's on the current page. So the code looks like:

Private Sub Detail1_Print(Cancel As Integer, PrintCount As Integer)
On Error Resume Next
strStringKey = Me.MyStringField
datDateKey = Me.MyDateTimeField
strEmail = Me!MyEmailControlName
End Sub

Step 3.) Now that the data is stored in the report in VBA we can use it with
the other functions we need to create for the report which are new public
functions and properties that can be called from an external function. The
first is a function to set the filter for the report so that when SendObject
is called, only the current group in view is output: So add a public
function like this:

Public Function SetFilter()
On Error Resume Next
If Len(strStringKey) > 0 And Not IsEmpty(datDateKey) Then
Me.Filter = "[MyStringField] = '" & strStringKey & "' AND
[MyDateTimeField] = #" & datDateKey & "#"
Me.FilterOn = True
DoCmd.RunCommand acCmdSave
End If
End Function

Step 4.) After the report is output to email, we need to clear the filter to
reset to the "old" full report view and return to the same page. Here's the
code to add to the report's module:

Public Function ClearFilter(intTargetPage)
On Error Resume Next
Me.Filter = ""
Me.FilterOn = False
DoEvents
DoCmd.RunCommand acCmdSave
DoEvents
Me.Page = intTargetPage
End Function

Step 5.) Last we need a method to get the current page of the report we were
on before setting the filter and sending the mail, so we create a public
property that we can call to obtain that:

Public Property Get CurrentPageNum()
CurrentPageNum = Me.Page
End Property

Step 6.) We also need a way to get the email address from the report so that
we can use that when we call send object so add code like this:

Public Property Get CurrentEmail() As String
CurrentEmail = strEmail
End Property

B.) Now that the report has ability to give us the data we need to use in an
external function to send the mail, the easy SendWholeReport function
outlined above is modified to look like this and it is placed in an general
module:

Public Function EMailCurrentRptGroup()
On Error Resume Next
Dim strRptName As String
Dim strEmailAddress As String, intPage As Integer
strRptName = Screen.ActiveReport.Name
If Len(strRptName) > 0 Then
strEmailAddress = Reports(strRptName).CurrentEmail
If Len(strEmailAddress) > 0 Then
intPage = Reports(strRptName).CurrentPageNum 'get the current page
in view
Reports(strRptName).SetFilter 'apply the filter for the current
group
DoEvents
DoCmd.SendObject acSendReport, strRptName, acFormatRTF,
strEmailAddress _
, , , "Here's Your Report", "Attached is an RTF Report", True
DoEvents
End If
Reports(strRptName).ClearFilter (intPage)
End If
End Function

C.) Then create a new custom toolbar button on your preview toolbar in the
the toolbar button's properties for the On Action property specify =
EmailCurrentRptGroup()

That will do it.
--
Steve Arbaugh
ACG Soft
http://ourworld.compuserve.com/homepages/attac-cg





marc said:
Hello,


I want to trigger from within a report an email send.

How can I put a command button (or other type of
button) on the report to do a Docmd.sendobject
from the inside of the report ?


Thank you in advance.
M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top