UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Dynamically Resize Continuous Dialog Form To Match Number Of Records    
Dynamically Resize Continuous Dialog Form To Match Number Of Records

A standard setup for a dialog form is usually a header area, some information in the middle and at the bottom we want to have a save, ok or close button. However if you use a Continuous Form, access doesn't seem quite able to resize it properly as can be seen from the image below, the purpose of the displayed form is that the user shall select one of the records and click the save link. As you can see however there is an excessive amount of white space, and said white space moves the buttons down where I don't feel they belong.

Form before resizing

This Image was meant to show how the form looks before the resize

To solve this issue I came up with a bit of code that will count the number of records, and size the form accordingly. The code will take into account the forms header and footer height, take the height of the detail section and multiply by number of records, finally adding a fixed size to account for the size of the forms title bar. The code also has the option to specify a max amount of records to display at a time, and if the amount of records in the form exceeds the max amount, the form will size to the max amount, and add a vertical scroll bar.

CODE
Private Sub Form_Load()
  'Variables
     Dim lngCount As Long
     Dim lngWindowHeight As Long
     Dim lngOldWindowHeight As Long
     Dim lngDeltaTop As Long
     Dim lngMaxRecordsToShow As Long

     lngMaxRecordsToShow = 10

  'Find the amount of records in form
     Dim rs As DAO.Recordset
     Set rs = Me.RecordsetClone
     If Not rs.EOF Then rs.MoveLast
     lngCount = rs.RecordCount

  'Check whether there are more then Max amount of records
     If lngCount > lngMaxRecordsToShow Then
        lngCount = lngMaxRecordsToShow
        'Enable vertical scrollbar
           Me.ScrollBars = 2 'Vertical
     Else
        'Disable scrollbars
        Me.ScrollBars = 0 'None
     End If

  'Calculate new windowheight.
  'If you do not have a header/footer, or they are not visible adjust accordingly
     lngWindowHeight = Me.FormHeader.Height + _
                 Me.Detail.Height * lngCount + _
                 Me.FormFooter.Height + _
                 400
                 'The 567 is to account for title bar Height.
                 'If you use thin border, adjust accordingly

  'The form will be "shortened" and we need to adjust the top property as well to keep it properly centered
     lngOldWindowHeight = Me.WindowHeight
     lngDeltaTop = (lngOldWindowHeight - lngWindowHeight) / 2

  'Use the move function to move and resize the form to the specified values.

     Me.Move Me.WindowLeft, Me.WindowTop + lngDeltaTop, , lngWindowHeight

  'Cleanup
     Set rs = Nothing
End Sub

After Resize

Below you can see an example of how the form looks now that it has been sized to fit the contents. This Image was meant to show how the form looks after the resize

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 4,280 times.  This page was last modified 07:59, 6 October 2012 by TheSmileyCoder. Contributions by TheDBguy  Disclaimers