[ Team LiB ] |
Recipe 8.1 Accelerate the Load Time of Forms8.1.1 ProblemThe first time you open a form in your application, it seems to take forever to load. Is there any way to accelerate this? 8.1.2 SolutionYou can radically improve the time it takes to load a form for the first time by preloading your forms when the database is initially opened. You can also decrease the load time for subsequent loadings by hiding instead of closing forms. This solution shows you how to improve form load time using these techniques. Load the 08-01.MDB database. Note the time it takes for the switchboard form to appear (see Figure 8-1). Make sure that the "Preload and keep loaded forms" checkbox is unchecked; if it's checked, uncheck it, close the database, and start over. Now press one of the command buttons, such as the Orders button, and note how long it takes Access to initially load the form. Close the form. Figure 8-1. The 08-01.MDB switchboard formNow check the "Preload and keep loaded forms" checkbox on the switchboard form and close the database. Reload the database and again note the time it takes for the switchboard form to appear. Load the Orders form, again recording the form load time. You'll see that the switchboard form now takes longer to appear but that the subsequent form load time is significantly shorter. That's because checking the "Preload and keep loaded forms" checkbox and reloading the database flips an internal switch that causes the application to preload its forms (in a hidden state) as the switchboard form is loaded by Access. This lengthens the time it takes for the switchboard form to appear initially. However, because the Orders form is now preloaded, it takes less time for it to appear when you press the Orders command button.
Follow these steps to set up your application to preload its forms:
Figure 8-2. Store the list of preloaded forms in the zstblPreloadForms table
Figure 8-3. The splash form, frmSplash
Figure 8-4. The database Startup dialog
8.1.3 DiscussionAccess forms are stored as binary data in hidden system tables in your database. When you load a form, Access reads data from the system tables to recreate and display that form. This takes time. The solution described here improves the application load time of forms by preloading them when the database is first loaded. This means that the initial application load time will be slower, but users are more tolerant of a long application load time because it is a one-time commitment. As with most performance optimizations, the benefits of this technique are especially noticeable on slow machines. Prior to Access 95, you had to use an AutoExec macro to initiate some action upon database startup; in recent versionsyou can use the Startup dialog to specify a form to be opened when the database is loaded. This solution takes advantage of the Startup properties, but you also could have used an AutoExec macro. When the switchboard form opens, the form's Open event is triggered and the code attached to the Open event is executed. Unfortunately, when the Open event procedure is called, the form has not had time to paint itself, so users normally see nothing during the Open event procedure. To remedy this, we created a "splash" form to display during the potentially lengthy process. You don't have to make the splash form the same size as the switchboard form, but in this case, we made the two forms very similar in appearance. The code to preload the forms is shown here: Set rst = db.OpenRecordset(acbcPreloadTable) Do While Not rst.EOF varFormName = rst("FormName") If Not IsNull(varFormName) Then DoCmd.OpenForm FormName:=varFormName, _ WindowMode:=acHidden, OpenArgs:="StayLoaded" End If rst.MoveNext Loop Each record from the zstblPreloadForms table is read and the named form is loaded in hidden mode. In addition, the form's OpenArgs parameter is passed the string "StayLoaded". You can use the OpenArgs parameter of OpenForm to pass a custom string to a form, much as you pass parameters to a function. This OpenArgs parameter will be used later to decide what to do when the preloaded form is closed. Once the forms have been loaded in a hidden state, you don't need to do anything special to make them appear. Access is smart enough to make a hidden form visible when you attempt to load it, which makes working with invisible forms easy. However, we include wrapper functions for opening and closing your application's forms in case you want some forms to be treated differently. For example, you may not wish to preload and keep all your forms loaded, because they will take up memory. Like the Form_Open event procedure attached to the switchboard form, the acbOpenForm function passes the string "StayLoaded" to a form via its OpenArgs argument when you pass True as the function's second parameter. Closing the application form is then handled by acbCloseForm, which is called by the Click event of each form's Close button. This function determines whether to close or hide the form by checking its OpenArgs property, which was passed to the form when it was opened: If InStr(frmToClose.OpenArgs, "StayLoaded") > 0 Then frmToClose.Visible = False Else DoCmd.Close acForm, frmToClose.Name End If For forms that you do not wish to preload, don't add them to zstblPreloadForms. For forms that you wish to close normally when the Close button is pressed, open them using the following syntax: =acbOpenForm("formname", False) If you have enough memory, you may wish to preload all forms and not close them until the application exits. In some situations, however, you may wish to be more selective. By using the preload technique and the acbOpenForm and acbCloseForm functions throughout your application, you can easily change your mind or customize form preloading and form hiding for different requirements. We did not remove from each sample form the Close button and control box provided by the system. This means that you can use one of these alternate mechanisms to bypass the application-defined Close button (and the acbCloseForm function) and close the form instead of hiding it. Thus, you may wish to set the CloseButton and ControlBox properties of your forms to No to prevent the use of these mechanisms. You may wish to make zstblPreloadForms a hidden table. You can adjust the hidden property of an object by selecting View Properties.
|
[ Team LiB ] |