Store the Sizes and Locations of Forms

Problem

Your application uses a number of forms that you can move around the screen. You’d like to store the last location away somewhere so that the forms will appear in the same location the next time you start the application.

Solution

Some Windows applications are “smart” and can save the locations of their windows when they exit. Your application can do this, too, using the system registry. You can store settings when you close a form and read them back the next time you open it.

Open and run the form frmSavePos in 02-10.MDB. Move it around the screen, and perhaps resize it. When you close the form, code attached to the Close event will save its coordinates in the system registry database. When you reopen the form, if the form can find the keys in the registry, it’ll reload the last set of coordinates and resize/position itself accordingly.

To use this technique with your own forms, follow these steps:

  1. Import the module basSaveSize from 02-10.MDB into your own application. This module contains the functions necessary to save and restore a form’s size and location in the registry.

  2. Add the following code to your form’s Load event procedure. This will restore the form’s size and location when you load the form:

    Private Sub Form_Load (  )
       acbRestoreSize Me
    End Sub
  3. Add the following code to your form’s Unload event procedure. This will save the size and location when you close the form:

    Private Sub Form_Unload (Cancel As Integer) acbSaveSize ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.