In Friday’s message, I was working on determining the height of the detail section with a Text Box control that has the CanGrow property set to “Yes”.
It is possible to determine this when displaying the report in “Print Preview” mode using the OnFormat event or the OnPrint event, but in “Report View”, the report never fires those events. Report View is typically used to display your report on a screen and in my case, it’s used within the subreport control on my form that I want to dynamically resize.
I have dynamically resized other on screen subreports like this using controls inside the report to sum the heights of the Report Header section, Detail section, Group Header and Footer sections, and so on, but so far I had not found a way within Access to natively find the height that the detail section had grown when a text box has a long string that wraps to multiple lines.
I had 2 possible thoughts on solving this.
- Try to launch a hidden version of the report in Print Preview mode and get the value of the control.
- Use Windows APIs to determine how many lines the control will wrap to and get the height of that number of lines in twips in order to use that to calculate what the height of the report will be.
Option 1 sounded easier, but I was concerned about the speed. I was thinking it would probably entail unloading the subreport control, displaying the report in print preview mode, getting the total report height, closing the print preview window, then reopening the report in the subreport control. I wasn’t sure I’d be able to do this with Echo Off because I’ve had issues with exporting to PDFs and needing to turn Echo to On in order to do so. That may not have been an issue. I ended up not trying this yet.
Option 2 I already had some example code for from the Access Enhanced MessageBox code generously provided for free. Inside this code base there is some code written to resize a form control based on the text inside given a width in twips. I extracted the code to test in my db.
So far I am still struggling to get it to work completely. It is not actually correctly getting the right size for every line yet, but even so, for some reason, my function to resize a subform/subreport control in my library is throwing an error when trying to get the value of the TotalReportHeight control in the footer of my report. It displays the value properly, but the function complains that it is returning some kind of invalid object.
I still have some ideas on getting around this, such as calculating the values based on the recordset and then just stuffing the final value into the control instead of using a function. I’m not sure if that will end up getting around this or not, but I will report back tomorrow whether I am successful or not.