Question

I currently have my form that has a field that, when clicked, executes some VBA code in an Event Procedure that opens a report:

DoCmd.OpenReport "GirlScoutandTroopMatchNoParams", acViewPreview, , "[TroopLeaders.Troop]=" & Me.Troop, acWindowNormal

What I need to do now is automatically generate an email and attach that report to the email.

Is there a way to do this?

Brand new to Access development, so don't assume I know anything :)

Was it helpful?

Solution

The following code will convert the report to PDF and attach it to an new email using your default email client:

gTroop = Me.Troop 'global variable to be defined in global module and accessed from the query using a function
DoCmd.OpenReport "GirlScoutandTroopMatchNoParams", acViewPreview, , "TroopLeaders.Troop=" & Me.Troop
DoCmd.SendObject acSendReport, "<NameOfReport>", "PDF", "johnbloggs@gmail.com"

You can use some more parameters to include subject and/or body text and/or to denote whether or not the email gets sent without showing in the email client.

The problem is that SendObject does not provide an option for specifying criteria! Therefore you have to write the report's underlying query, in such a way that it can get the criteria from the global variable.

Public Function GetTroopValue() as long
   GetTroopValue = gTroop
End Function

Then in the underlying query you will use:

SELECT ... WHERE Troop = GetTroopValue()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top