Monday 12 October 2015

Changing the zoom on a report depending on the number of pages

When you preview a report in Access, by default you are zoomed in on it. If the report is only one page long, I quite like to have the whole page showing instead. There is a neat way of doing this with just one line of code in the reports OnPage event. I like to bundle this with code to maximise the report so that it is displayed to its best advantage, like this:

Private Sub Report_Page()
    If Me.Pages = 1 Then DoCmd.RunCommand acCmdFitToWindow
End Sub

Private Sub Report_Close()
    DoCmd.Restore 'Restores the database to its pre-maximised view
End Sub

Private Sub Report_Open(Cancel As Integer)
    DoCmd.Maximize 'Maximises the report preview
End Sub

This does the job, and can be adapted for other purposes too - for example, try replacing acCmdFitToWindow with acCmdZoom150

Anyway, this method replaces a previous fudge that made use of the annoying SendKeys command, which could cause your NumLock to toggle on/off mysteriously if you also used other SendKeys commands in your code, as explained here.

No comments:

Post a Comment