On Load Event of Report (1 Viewer)

Tieval

Still Clueless
Local time
Today, 05:21
Joined
Jun 26, 2015
Messages
475
I have a very complex piece of VBA generating a report, for what it is worth the details are below:
Code:
Private Sub Report_Load()

'Populate results according to criteria on main form
Dim scandata As String
Dim OrderBy As String
Dim BladesOnly As String
Dim SearchStg As String
SearchStg = ""

   scandata = "SELECT  Scans.Scanned, Len([Scans]![Blade]) AS Expr1, " & _
                "Scans.ID, Scans.Blade, Scans.Engine, " & _
                "Scans.Part, Scans.Hours, Scans.Cycles, " & _
                "Scans.Result, Scans.Tank, Scans.Operator, " & _
                "Scans.Details, Scans.Co, MRO.MRO " & _
                "FROM Scans INNER JOIN MRO ON Scans.Co = MRO.ID " & _
                "WHERE (Scans.Scanned BETWEEN Forms!frmMain!tStartDate And Forms!frmMain!tEndDate +1)" & _
                "AND Scans.Co LIKE (Forms!frmMain!cboMRO)" & _
                "AND Scans.Result LIKE '*' &(Forms!frmMain!tStatus) & '*' "
                
                'Filter Combo and Entry
                If Forms!frmMain!Search = 1 Then
                SearchStg = "Scans.Engine LIKE '*' & [Forms]![frmMain]![tSearch] & '*'"
                ElseIf Forms!frmMain!Search = 2 Then
                SearchStg = "Scans.Part LIKE '*' & [Forms]![frmMain]![tSearch] & '*'"
                ElseIf Forms!frmMain!Search = 3 Then
                SearchStg = "Scans.System LIKE '*' & [Forms]![frmMain]![tSearch] & '*'"
                ElseIf Forms!frmMain!Search = 4 Then
                SearchStg = "Scans.Operator LIKE '*' & [Forms]![frmMain]![tSearch] & '*'"
                ElseIf Forms!frmMain!Search = 5 Then
                SearchStg = "Scans.Hours >= (Forms!frmMain!tSearch)"
                ElseIf Forms!frmMain!Search = 6 Then
                SearchStg = "Scans.Cycles >= (Forms!frmMain!tSearch)"
                Else
                SearchStg = "Scans.Engine LIKE '*'"
                End If
                
                'Allow for sorting with the addition of date and time
                If Nz(Forms!frmMain!Check59.Value, 0) = -1 Then
                        OrderBy = "Scans.Scanned"
                Else
                        OrderBy = "DateValue(Scans.Scanned), Scans.ID"
                 End If
                 
                'Allow for blades only checkbox
                If Nz(Forms!frmMain!chkFan.Value, 0) = -1 Then
                    BladesOnly = "Scans.Blade Like '*' & [Forms]![frmMain]![tBlade] & '*' AND (Len([Scans]![Blade]))=8"
                Else
                    BladesOnly = "Scans.Blade Like '*' & [Forms]![frmMain]![tBlade] & '*'"
                 End If

                Me.RecordSource = scandata & " And " & SearchStg & " And " & BladesOnly & " Order By " & OrderBy
This works perfectly if I open the report but the load event is not called in automated operations such as:
Code:
Private Sub Command24_Click()
DoCmd.OpenReport "OperationsReport", acViewNormal
End Sub
Code:
Private Sub Command25_Click()
DoCmd.OutputTo acOutputReport, "OperationsReport", "PDFFormat(*.pdf)", , True, , , acExportQualityPrint
End Sub
How can you force the load event?
 

isladogs

MVP / VIP
Local time
Today, 05:21
Joined
Jan 14, 2017
Messages
18,209
You shouldn't need to do so. It may be that the event has got disconnected from the report. Check it exists in the report property sheet.

Or try moving that into a separate procedure and calling that from the load event. Add the line DoEvents following that to give the processor time to complete before moving onto other actions.

If you still have problems, try moving some of the code (where feasible) to the calling form so it runs before opening the report
 

JHB

Have been here a while
Local time
Today, 06:21
Joined
Jun 17, 2012
Messages
7,732
Can't you move it to the OnOpen event?
 

Tieval

Still Clueless
Local time
Today, 05:21
Joined
Jun 26, 2015
Messages
475
Thanks Ridders, I am beginning to see the problem. To make the report I used the base table as the record source and then generated the code in the on load event.

Opening the report by right clicking in the side pane of access causes the report to load using the on load event, either of the two automated methods don't use the on load event and just load from the original table so I get a report with everything in it.

I have proved this by removing the original data source and now on the automated methods it doesn't use the load event and just loads a row of fields with error in them as it is now has no original data source.
 

Tieval

Still Clueless
Local time
Today, 05:21
Joined
Jun 26, 2015
Messages
475
Can't you move it to the OnOpen event?

That actually works and many thanks. I am now somewhat confused as I guessed that the on load was more likely to come before the on open event :confused:

My terminology logic was load it into memory and then open it on screen, not open it into memory and then load to screen.
 
Last edited:

Tieval

Still Clueless
Local time
Today, 05:21
Joined
Jun 26, 2015
Messages
475
Thanks to Banana in 2006.
Consider the order events falls in when opening/closing form:

OnOpen -> OnLoad -> Activate (I think) -> OnCurrent -> DeActivate -> OnUnLoad -> OnClose

Now, because of the ordering, you can cancel a OnOpen load, but not Load (as it's already open), and you can cancel a UnLoad (because it's not removed from screen just yet) but not the OnClose.

Open and Close events has to do with "loading" the forms into the memory but they are not quite on the screen.

Load/Unload event is when forms actually shows on the screen.

Activate/Deactive is when forms has focus or lost focus.
 

isladogs

MVP / VIP
Local time
Today, 05:21
Joined
Jan 14, 2017
Messages
18,209
Ok I'm now confused.
Open event definitely comes before load
I'm not sure about the accuracy of the load event only occurring when the object appears on screen.

Thinking about it, I rarely use the report load event but always do so for forms

You can find details of other sequences at the MS site https://support.office.com/en-us/article/Order-of-events-for-database-objects-E76FBBFE-6180-4A52-8787-CE86553682F9

Interestingly it doesn't mention the load event for reports. So why does it exist?

The accepted advice is not to put complex code in the open event as the object hasn't fully loaded. That's particularly important if these reference controls on the form/report.

In such cases I use the load event for forms and the on format event for the report section holding the control(s)

I'm pleased its working for you but can't say I fully understand why.
 
Last edited:

JHB

Have been here a while
Local time
Today, 06:21
Joined
Jun 17, 2012
Messages
7,732
..
The accepted advice is not to put complex code in the open event as the object hasn't fully loaded. ...
That is correct, but it seems that the Load event isn't fired when the report is send directly to the printer. But the OP isn't manipulate any controls, only the report's recordsource is set.
 

isladogs

MVP / VIP
Local time
Today, 05:21
Joined
Jan 14, 2017
Messages
18,209
Strange the MS site makes no mention of it.
Its obviously more by luck than judgement that I don't use the Report_Load event. In fact its only recently I noticed it existed for reports!
 

JHB

Have been here a while
Local time
Today, 06:21
Joined
Jun 17, 2012
Messages
7,732
If you compare what is shown in the Help file about a report's UnLoad event, with what is shown in a report's OnLoad event, it gives some explanation, even though it is a bit vague. :)
From the Help-file:
The Unload event occurs after a report is closed but before it's removed from the screen.
(A printed report isn't shown a the screen.)

The Load event occurs when a report is opened and its records are displayed.
(A printed report doesn't display any records.)
 

isladogs

MVP / VIP
Local time
Today, 05:21
Joined
Jan 14, 2017
Messages
18,209
Thanks Jorn

Still odd that MS make no mention of it on the web page I quoted
 

JHB

Have been here a while
Local time
Today, 06:21
Joined
Jun 17, 2012
Messages
7,732
I totally agree.
The entire description for a report Load/Unload event is not clearly described. We only find the differences because we know what to look for, otherwise we would not notice it.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 05:21
Joined
Jul 9, 2003
Messages
16,271
I note there are problems with the order and abilities of a reports opening events.

That's why I came up with this method of handling a report record source.

Basically the record source is constructed in the calling form, the form that opens the report.

The SQL for the record source is stored in the form in a custom property and the reports grabs the record source from the opening form.

The form passes through its own name in the openargs, this allows you to open the report from any form you like without changing the construction of the report.

there are some videos and a Google presentation explaining the process in my blog here:-

Generate Multiple Reports - Nifty Access

http://www.niftyaccess.com/generate-multiple-reports/

With regard to the unexpected way that the loading events work, I haven't studied them in a report but I did study them in a form and I've blogged about it here:-

Form Load, Activate, Open, Issue, – Form Load Events

http://www.niftyaccess.com/form-load-activate-open-issue-form-load-events/

Sent from my SM-G925F using Tapatalk
 

Users who are viewing this thread

Top Bottom