What Report level fields are available to the 'On Print' event (1 Viewer)

GoodyGoody

Registered User.
Local time
Today, 08:43
Joined
Aug 31, 2019
Messages
120
Hi, I have a query with a number of columns that aren't printed on the report line but that I was to save to table for each line on the report. In the 'On Print' event I can successfully pick up this particular field (Me.RaceEventID as it happens) from the drop down that appears so Access VBA knows it's in the collection but when I actually run the report VBA gives an error saying it can't field the field Me.RaceEventID. I'd like to know why this is and so is the only work around to 'print' the field on the line but to hide it for printing to to make it explicitly visible to the 'On Print' event? Thanks as usual for any help.
Stephen
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:43
Joined
Sep 21, 2011
Messages
14,037
Me refers to the form or report.?
That control is not on your report.?
 

GoodyGoody

Registered User.
Local time
Today, 08:43
Joined
Aug 31, 2019
Messages
120
It's all referring to controls on the Report. The Me.RaceEventID field is on the query and is available to the collection within VBA as I can select it in VBA when I type me.etc . It is not explictly anywhere on the report yet hence my suspicion that I need to actually put it in the detail line and hide it so it is available to VBA. But then when I run it it comes up with an error and when I debug it, it says field not found. Thanks
Stephen
 

Minty

AWF VIP
Local time
Today, 08:43
Joined
Jul 26, 2013
Messages
10,353
On a report the fields have to be present to be able to refer to them, unlike a form.

I suspect it's to do with the reports ability to group and total fields on the fly which could get messy if they were only in the underlying query, as the grouping wouldn't have taken place at that point.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:43
Joined
Oct 29, 2018
Messages
21,357
Hi Stephen. Just curious, would you still get an error if you used bang instead of dot? For example: Me!RaceEventID
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,361
Stephen,

Can you post a copy of the database so readers can "experience" the issue?
We only need enough data to highlight the issue. You can anonymize a few records if there is some confidentiality involved.
 

GoodyGoody

Registered User.
Local time
Today, 08:43
Joined
Aug 31, 2019
Messages
120
Hi All, thanks for the replys. AS I suspected, it's because on a report the Query fields are available to the Collection but not the OnPrint event unless explcitly declared as Minty above said to. Thanks for the feedback. BTW, me!RaceEventID doesn't work either. Thanks again S
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Feb 19, 2002
Messages
42,970
Minty got it almost right.

In a Report, unlike a form, if you want to reference a field from the RecordSource in code, you must bind it to a control. You can make the control tiny and hidden but the field MUST be bound to a control to be available later. The reason is that Access is smarter than we are and for a report, it rewrites our query and substitutes its own version which includes ONLY the fields that are actually bound to controls. This is also why you can't rely on sort order in your RecordSource query but MUST use the Report's own sorting and grouping options to impose a sort order.
 

GoodyGoody

Registered User.
Local time
Today, 08:43
Joined
Aug 31, 2019
Messages
120
thanks for the explanation Pat Hartman. I do like to know why things are happening. Makes it much easier to remember for next time. :)
 

Users who are viewing this thread

Top Bottom