471,120 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,120 developers and data experts.

How to dynamically resize a continuous Dialog form to fit / show all records

2,322 Expert Mod 2GB
I have recently had a issue where I open a continuous dialog form, showing anywhere from 1 to X records, X usually being in the 2-5 range. I found it very annoying that Access2010 per default made the continues form take up the entire height of the screen, even when there is only a few records.

So to fix that issue I came up with a bit of code to make the window size roughly match the amount of records, with the ability to specify a max amount of records to show. If the amount of records exceed the max, I also use the code to modify the scrollbar settings, so that only if there are more then Max records the scrollbar is shown.

The code is shown below, I placed it in the Load event of the form:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    'Variables
  3.       Dim lngCount As Long
  4.       Dim lngWindowHeight As Long
  5.       Dim lngOldWindowHeight As Long
  6.       Dim lngDeltaTop As Long
  7.       Dim lngMaxRecordsToShow As Long
  9.       lngMaxRecordsToShow = 10
  11.    'Find the amount of records in form
  12.       Dim rs As DAO.Recordset
  13.       Set rs = Me.RecordsetClone
  14.       If Not rs.EOF Then rs.MoveLast
  15.       lngCount = rs.RecordCount
  17.    'Check whether there are more then Max amount of records
  18.       If lngCount > lngMaxRecordsToShow Then
  19.          lngCount = lngMaxRecordsToShow
  20.          'Enable vertical scrollbar
  21.             Me.ScrollBars = 2 'Vertical
  22.       Else
  23.          'Disable scrollbars
  24.          Me.ScrollBars = 0 'None
  25.       End If
  27.    'Calculate new windowheight.
  28.    'If you do not have a header/footer, or they are not visible adjust accordingly
  29.       lngWindowHeight = Me.FormHeader.Height + _
  30.                   Me.Detail.Height * lngCount + _
  31.                   Me.FormFooter.Height + _
  32.                   400
  33.                   'The 567 is to account for title bar Height.
  34.                   'If you use thin border, adjust accordingly
  36.    'The form will be "shortened" and we need to adjust the top property as well to keep it properly centered
  37.       lngOldWindowHeight = Me.WindowHeight
  38.       lngDeltaTop = (lngOldWindowHeight - lngWindowHeight) / 2
  40.    'Use the move function to move the form
  42.       Me.Move Me.WindowLeft, Me.WindowTop + lngDeltaTop, , lngWindowHeight
  44.    'Cleanup
  45.       Set rs = Nothing
  46. End Sub
I use:
AutoRezise= True
with this code. Odd results may occur if you do not, but you can go test that yourself.
Oct 5 '12 #1
14 45295
3,653 Expert Mod 2GB

Quite an interesting solution. Have you found any bugs or quirks with this code, as of yet? My experience with resizing forms has been trial and error, and sometimes the forms will just be the wrong size, even though I set the size. Very creative solution, though!
Oct 5 '12 #2
2,322 Expert Mod 2GB
Not as of yet, as I just wrote the code for the first time today. I looked around online, but couldn't find any similar solution, so figured I should share my approach.

Problems I imagine might occur, is if you leave the MaxNrOfRecords very high, and thus the total height of the window overflows some maxHeight on the window.
Oct 5 '12 #3
32,349 Expert Mod 16PB
I did something similar a while back and it took me ages to realise that to resize a form you had to move it. Not very intuitive, but I got there eventually. I was convinced for ages that resizing simply didn't work.
Oct 22 '12 #4
2,322 Expert Mod 2GB
I had to laugh a bit at that (not you) cause I have been there myself. Thankfully Google and Bytes were there to help me :)
Oct 22 '12 #5
6 Nibble
Cool idea Smiley. I've used this twice now with great success. In setting the window height, I added to the lngWindowHeight calc:

... + IIf(Me.NavigationButtons = True, 250, 0) + ...

in case the form has the navigation buttons enabled.
Apr 10 '13 #6
2,322 Expert Mod 2GB
Nice touch Hoopla, thank you very much for reporting it back. I shall try to have a look at it soon, and then update the original post.
Apr 14 '13 #7
32,349 Expert Mod 16PB
In setting the window height, I added to the lngWindowHeight calc:
Where's that "Like" button when you need it? Good idea :-)
Apr 14 '13 #8
6 Nibble
I just noticed today that this doesn't work in Access 2013 as well as it does in 2010. It may be that the window height is returned differently in 2013 with the flat metro styling. Does anyone know a quick fix?

The attached image shows how the sizing differs between two versions of Access but the same application.

I just realized and should note that the Access 2013 image was taken from Windows Server 2012, which may be an additional factor.

Attached Images
File Type: png Access_2010_vs_2013_sizing.png (27.9 KB, 6418 views)
Jun 5 '14 #9
Seth Schrock
2,965 Expert 2GB
This is a great help. I'm always fighting with the height of my continuous forms. Everytime I go into design view and back to form view, I have to fix the height of the window. Thanks Smiley.
Jun 7 '14 #10
Thank you very much for your solution, works like a dream. It is really annoying the height of those Continuous forms. Awesome !!
Jan 13 '16 #11
2,322 Expert Mod 2GB
Glad you liked it. Thanks for taking the time to let me know :)
Jan 13 '16 #12
Seth Schrock
2,965 Expert 2GB
I have just run across a bug with this. I have a form in which I set the record source in the form's OnLoad event based on selections from the previous form. After I do that, I then call your function, but I get error 7159 saying that I have made an invalid reference to the object's RecordsetClone property. I tried changing your code to instead use the Record Source property to create a recordset, but then I get an error saying that the OpenRecordset() function can't find the recordset ''. Any suggestions on how to get around this?
Apr 15 '16 #13
1 Bit
@Seth Schrock
It's been a long time since Seth posted this question so he's probably figured it out by now but I may have an answer.
I got this same error. It turned out that the recordset was empty or null. It had not been initialized so it could not be cloned.
Aug 7 '21 #14
32,349 Expert Mod 16PB
Hi DJ.

Please remember that it is never appropriate to add your own question into an existing thread. This is termed hijacking and is outlawed here (as well as pretty well everywhere of course). If you need help then post a thread in the normal way. You are allowed to reference existing threads.

This is even more true for article threads. Who wants to go through all the dross when they're trying to learn something specific. It's like barnacles on the hull of a ship.
2 Weeks Ago #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Noah Coad [C# MCP] | last post: by
1 post views Thread by andrew | last post: by
3 posts views Thread by Guadala Harry | last post: by
7 posts views Thread by Peter | last post: by
14 posts views Thread by Simon Abolnar | last post: by
1 post views Thread by Teelions | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.