Close current op Report from Button and open subsequent report (1 Viewer)

JMHScot

New member
Local time
Today, 14:03
Joined
Sep 1, 2022
Messages
11
Hi, I have database with a form, frmPrimary, which is designed as my Switchboard of sorts. The database contains mountain classifications and reports. If I want to view the data for any classification in form view, then I have a button for each classification, eg, btnMunros, btnCorbetts which, when clicked have the following Event Procedure to a) close any currently open forms, and b) open the selected form as defined by the button. The OnClick Event Procedure on each button is shown below. (DoCmd “frmMunros” varies of course for each button)

Private Sub btnMunros_Click()

Dim obj As Object

Dim strName As String

For Each obj In Application.CurrentProject.AllForms

If obj.Name <> "frmPrimary" Then

DoCmd.Close acForm, obj.Name, acSaveNo

End If

Next obj

DoCmd.OpenForm "frmMunros", acFormDS

End Sub

This saves users having to close the current form before they open another one, as this procedure is repeated for each button OnClick. A “Home” button simply clears the screen if no other form is required.

These procedures all work, and have worked for some years, no problems.

Now, there is a requirement to have a similar procedure for when Reports are also being viewed, so, I was happy with that, I simply created additional buttons with the same procedure, only defining for Report, and expecting the same flawless operations. No such luck. Whichever classification is selected first, then the report displays as intended. However, when clicking on a subsequent report button, the open/active report simply beeps, shudders and stays stubbornly on the screen, and the newly selected report does not open.(If I close the open/active report with the standard report close button, it closes fine) The procedure called is shown below.

Private Sub btnMunrosABC_Click()

Dim obj As Object

Dim strName As String

For Each obj In Application.CurrentProject.AllReports

If obj.Name <> "frmPrimary" Then

DoCmd.Close acReport, obj.Name, acSaveNo

End If

Next obj

Me!txtStatus = "Munro"

ReportPage = 1

DoCmd.OpenReport "rptMunroABC", acViewReport

Me!txtStatus.Value = Null

End Sub

I know it is only an additional click to simply close each report as standard and then click the button to open the next report. But heck, isn’t that what we all want, the one click option. Thanks
I should add, I have viewed and tried a number of ways regarding my question, but none seem to work and close the report.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:03
Joined
Oct 29, 2018
Messages
21,477
Is there any code behind the report's Unload event?
 

JMHScot

New member
Local time
Today, 14:03
Joined
Sep 1, 2022
Messages
11
No. Unload event is empty/blank.
 

JMHScot

New member
Local time
Today, 14:03
Joined
Sep 1, 2022
Messages
11
Okay, so here is a test version I was using, just a few records, forms and reports. On the frmPrimary, which will launch on opening, the Blue buttons are those linked to forms, click any they automatically open selected form, and if a form is already open, close it, including the Search option. Where it all comes unstuck is when I added the two test Black buttons linked to the reports, they should function as do the Blue ones, but as you will see, they open a report okay, but fail to close any open reports. Any thoughts on what I am doing wrong. Thanks
 

Attachments

  • SampleDB.accdb
    3.1 MB · Views: 78

theDBguy

I’m here to help
Staff member
Local time
Today, 06:03
Joined
Oct 29, 2018
Messages
21,477
Okay, so here is a test version I was using, just a few records, forms and reports. On the frmPrimary, which will launch on opening, the Blue buttons are those linked to forms, click any they automatically open selected form, and if a form is already open, close it, including the Search option. Where it all comes unstuck is when I added the two test Black buttons linked to the reports, they should function as do the Blue ones, but as you will see, they open a report okay, but fail to close any open reports. Any thoughts on what I am doing wrong. Thanks
Hi. Thanks for posting a sample db. Sorry if I may have messed it up. Hopefully, you have a backup copy.
 

Attachments

  • SampleDB (1).zip
    47.7 KB · Views: 71

JMHScot

New member
Local time
Today, 14:03
Joined
Sep 1, 2022
Messages
11
Ok, so that works, but I am feeling exceedingly dumb. I don't see any difference in my Even Procedure, or am I missing sometheing, or did you change a setting elsewhere? Can you please point/explain to me to what you have done so I know for transfer to master database and future use. Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:03
Joined
Oct 29, 2018
Messages
21,477
Ok, so that works, but I am feeling exceedingly dumb. I don't see any difference in my Even Procedure, or am I missing sometheing, or did you change a setting elsewhere? Can you please point/explain to me to what you have done so I know for transfer to master database and future use. Thanks
Hi. Glad to hear it working the way you wanted. The problem you were having was caused by having all your reports as Modal = Yes. I guess it's fine with forms, but doesn't seem to work with reports. So, all I did was changed the Modal property of both reports to No. Cheers!
 

Users who are viewing this thread

Top Bottom