Update Header When Two Buttons Open The Same Report

OasisUnknown

New Member
Joined
Oct 15, 2015
Messages
46
Hello Everyone,

So far this forum has been an excellent resource so I thought I would try another question for the group.

I have a report that displays information about employees.

On a main menu form I have two different buttons that both open this one report about the employees.

What I am trying to do is update the header label depending on which button gets pressed.

currently I am doing this by passing the label I want through the open args parameters.

button one
Code:
DoCmd.OpenReport "myRpt", acViewReport, , "[CD Coded] = true", acWindowNormal, "True Code Rpt"

button two
Code:
DoCmd.OpenReport "myRpt", acViewReport, , "[CD Coded] = false", acWindowNormal, "False Code Rpt"

individually these two buttons work fine on their own and the label updates.

My question and problem comes when the report is already open and button 2 gets clicked.

The report data will open and update correctly (ie I get the false report) but the label never updates.

I am assuming this happens because I have the label updating on the reports on load procedure and because the report is already loaded the new label never gets updated.

I have also tried the on open procedure but this does not run the label update code either.


A work around I have created was a pre check open method that checks to see if the report is loaded and if it is then it closes the report.

Code:
Public Sub PreCheckOpenReportAndClose(targetReport As String)
On Error GoTo Error_Handler

    If Application.CurrentProject.AllReports(targetReport).IsLoaded = True Then
        'close report
        DoCmd.Close acReport, targetReport
    End If
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    Call DisplayErrorMessage(targetReport)
    Resume Error_Handler_Exit
End Sub

but to me this is clunky.
I have to call the preopen method and then open my report in every instance that I want to open a report.

Any one have any suggestions on how I could do this better?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
That is what I had to do for similar reasons, but mine was related to showing new data. If you're sure you don't need to requery the report (i.e. the data has not changed) you could try the RepaintObject method. It's an old, backwards compatible method which I've never used, but it might work.

Code:
If Application.CurrentProject.AllReports(targetReport).IsLoaded Then
  Reports!targetReport.LabelName.Caption = Me.OpenArgs
  Docmd.RepaintObject acReport, "targetReport"
Else
open report as you were doing before
End If

Note that = True is not needed in this (and similar) cases, and I'm assuming the open args are available when this runs.
 
Upvote 0

Forum statistics

Threads
1,221,776
Messages
6,161,870
Members
451,727
Latest member
tyedye4

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top