Report events not being triggered

kingsbishop
kingsbishop used Ask the Experts™
on
Question by:  Pelegrinus On 2012-08-07 01:24 PM https://www.experts-exchange.com/questions/27820593/Opening-an-Access-Report-in-acViewNormal-Doesn't-Trigger-Report-Events.html
I hope this is enough information to let you know the question I am responding to. What I stumbled on is that if a global variable is used within the on open procedure the code will execute even when a report is printed. In a module I placed the following code:
global gbdone as boolean
then inside the on open procedure I placed the the following code, along with the other code I wanted to execute:
gbdone = false
and every thing executes

Thanks for making all this information available to us. (Microsoft sure is not much help most of the time.)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014

Commented:
That's interesting and I'll have to check out its implications.
The original was here
http://www.experts-exchange.com/Database/MS_Access/Q_27820593.html
It's not that the Report_Open code doesn't function at all, but breakpoints definitely don't work, and attempts to set .Caption fail -- and since .Caption is the name given to the job in the print queue, and the default name given to the PDF file -- being able to set .Caption successfully is pretty damn important!

I have done that by opening with DoCmd.OpenReport stDocName, acViewPreview and, with the Report open, then printing it out -- but it is more time-consuming than a straight DoCmd.OpenReport stDocName, acNormal to do it that way.

Interesting!
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012
Commented:
FWIW....

This thread may be a bit misleading...

Setting the Global variable does noting to force "Formatting" code to work.
ex:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.Junk <> "Bob" Then
    Me.Junk.BackColor = vbRed
Else
    Me.Junk.BackColor = vbWhite
End If
End Sub

What I mean is, ...
The title of this question, and the question it references, ...makes it seem like like you have created a workaround for forcing any code to run in Report View.

Just FYI...
Most Valuable Expert 2014
Commented:
Agreed.  This has only to do with what happens to the Open() event code when a report is sent directly to the printer via DoCmd.OpenReport stDocName, acNormal

global variable is used within the on open procedure the code will execute

But that's been a problem for a long time, if you change record sources via OpenArgs, or set dynamic captions so that PDF rendering gets a unique filename, that you had to open in Preview and then set off printing.

Touch a global variable in Open() and the full Open() event code executes regardless of how the report was opened.

And THAT is very nice to know -- because the print queue job names are also defaulted to the .Caption.
So it is very sweet to finally be able to get unique names to such in things in all situations.

I have a report without a caption named rptScratchSheet.
I can put this code in the Report_Open(Cancel as Integer) event
Private Sub Report_Open(Cancel As Integer)
Me.Caption = Forms!frmTravellers!JobID.Column(1) & " Scratch Sheet"
End Sub

Open in new window


But if I print it (or export to PDF) via DoCmd.OpenReport stDocName, acNormal
The caption is not set and the print queue job name and PDF default filename remain rptScratchSheet

This code, when myDummy is a global variable in a standard module
Private Sub Report_Open(Cancel As Integer)
MyDummy = False
Me.Caption = Forms!frmTravellers!JobID.Column(1) & " Scratch Sheet"
End Sub

Open in new window

Does cause the .Caption to be set.

Very nice discovery!
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
Yes,
Very interesting...
Most Valuable Expert 2014

Commented:
Close it with zero points.
It wasn't a Q per se, but there is content here worth preserving

Nick67

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial