ms-accessvbams-access-reports

How to repaint and resize a subreport control in MS Access VBA


I have a parent report called rptClientCareAll. It is made up of 3 subreport controls. The user has the option to hide the detail of one of the subreports (if it's relevant, this is the code used to hide the subreport's detail: me.detail.visible = false).

Problem #1: When I open the subreport independently of the parent report (sometimes the report will be shown as an independent report and sometimes shown as a subreport, so I need it to work in both situations), the detail is hidden, but the report is not "repainted"--so, the report looks a mess because the footer is now shown right below the header but the detail is not "erased" from the screen. How can I "repaint" the report so that the detail is removed? It's possible to repaint forms, but I cannot seem to repaint a report.

Problem #2: When I open the parent report and then opt to hide detail on the subreport, the detail is correctly hidden and "erased" (unlike in Problem #1 above; weird!), but, the subreport does not shrink in size, so, the whole point of hiding the detail is lost because there's this huge span of blank space on the parent report where the detail of the subreport used to appear. How can I get the subreport to shrink when the detail is hidden? I've tried setting all reports' (both parent and subreports) detail sections' Can Grow and Can Shrink properties set to Yes, but that doesn't help.

Note: I am showing the reports in acReportView, not acViewPreview. Seems like that might make a difference to what events are triggered?


Solution

  • So, basically Problem #1 (report is opened independently, not as a subreport) is solved by a simple Me.Requery after the user opts to hide the detail section.

    Problem #2 apparently cannot be solved without closing and reloading the report, which was a bit easier to say than to do in my case.

    For my situation, I added code to do the following:

    1. When the user clicks to toggle the detail of the subreport to hidden/visible, (a) set a global variable flag (boolean type) to indicate whether the detail section is now hidden (false) vs. visible (true), (b) set a different global variable to indicate that the report needed to be re-opened, and (c) close the report.
    2. In my case, in the Close event of the parent report, I had to open a form when closing the report because I needed the form to re-open the report (if the global variable for re-opening the report was set to True) once it was closed because I could not figure out a way to close and re-open the report from within itself. In the form, I set a timer event to trigger after 333ms (TimerInterval = 333) that re-opens the report and closes the form itself. (When I tried to re-open the report in the form's Open event, the report would never actually open, so I added the timer to delay things long enough to allow the report to close completely before trying to re-open it.) (Of course, if the global variable for re-opening the report is False, then the form just closes.)
    3. In the subreport whose detail section is toggled between hidden/visible, in the Open event, use Me.Detail.Visible = {globalvariableflag} and set the global variable for reloading the report to False (since the report has now reloaded).

    It's clunky, but it was the only way I could find to accomplish hiding and shrinking the detail section on demand.