Opening an Access Report in acViewNormal Doesn't Trigger Report Events

Pelegrinus
Pelegrinus used Ask the Experts™
on
Using VBA, I can easily open a report based on a query. I normally want to open the report simply to print it, rather than to view it on the screen.

Sometimes I use a WHERE clause to limit the data in the report. When I use a WHERE clause, I want to change a label in the PageHeader to show what the limit (the filter) is. E.g., for one WHERE clause, the label caption might need to show, "Students for Teacher Jane Doe," and for another WHERE clause, "Students for Techer John Smith."

I use this code to open the report:

docmd.OpenReport "rpt_R010_StudentListing", acviewNormal,,"lngTeacher=5".  

If I open the report this way, no events trigger and the report simply prints without the necessary change in the label caption.

If I open the report using acViewPreview, however, events trigger in the report, and I can easily change the label caption. For example, on PageHeader.Format event, I can say, "lblPageHeader.Caption = 'Students for Teacher Jane Doe.'"

Because I'm developing this process for other users, I don't want a preview of the report; instead I simply want to print it directly.

Am I doing something wrong? Or do I need a work-around? If I need a work-around, can anyone provide any suggestions?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I typically pass the filter as the OpenArgs property to the report, and them set the Caption using that. For Example:

docmd.OpenReport "rpt_R010_StudentListing", acviewNormal,,"lngTeacher=5", ,"Students for Techer John Smith."

and then use then Open event to set:

    lblPageHeader.Caption = Me.OpenArgs
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
<<Am I doing something wrong? Or do I need a work-around? If I need a work-around, can anyone provide any suggestions?>>

  Your not doing anything wrong.  "Report view" is not the same as Print Pre-view or Printing and it doesn't fire any events (which makes it basically worthless as if you have any code in your report, it probably won't look like your report<g>).

 Use a form, which you hide when you view, to hold your criteria and have the report controls reference the form and controls directly.

  OR

  have the controls call a function, which supplies the values required.  If have a get/put set of functions which is nothing more then array.   Put item(x), call the report, which uses a Get item(x) to fetch the value.

Jim.

Commented:
Or instead of the open event, you can just set the source of the caption to be Me.OpenArgs

Author

Commented:
I applied this solution (setting the Open event to lblPageHeader.Caption = Me.OpenArgs), which works even though I cannot trap the event by putting a break in the code. I'm always happy with a solution that works, but I wish I understood what was going on. On the hand, why should this be different from most of my life? :)

I could not try your second proposed solution because I couldn't find any way to do it in the form design window.

Commented:
Thank you for accepting my solution.

I am not sure why you cannot get a break in the code to work. Does it not stop on the break?

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