|[ Team LiB ]|
Recipe 9.10 Create a Generic, Reusable Status Meter
Access allows you to control the built-in status meter using the SysCmd function, but you have no control over the location or appearance of this status meter. How do you create a status meter that you can control?
You can create a status meter based on an Access form and control it using VBA routines. The status meter is composed of a Rectangle control and a Label control. By updating the Width property of the rectangle, you can control the meter's progress. Additionally, by updating the Caption property of the label, you can insert messages such as "50% complete." All the internal workings of the control can be encapsulated (hidden) inside the form.
For an example of a programmatically controlled status bar, open and run frmTestStatusMeter from 09-10.MDB (see Figure 9-32). To start the status meter, click the Start button and frmStatusMeter will pop up. If you want the status meter to include a Cancel button, check the Include Cancel button checkbox before clicking the Start button. The status meter will slowly advance to 100% and then close. If you've included a Cancel button, you can click on it at any time to immediately close the status meter and notify the calling form (frmTestStatusMeter) that the cancel has been requested.
184.108.40.206 Create a generic status meter
To create a generic status meter for your own application, follow these steps (or skip these steps entirely and import frmStatusMeter and basStatusMeter from 09-10.MDB into your database):
220.127.116.11 Use the generic status meter in your application
To use the generic status meter in your own applications, follow these steps:
You can change the size of the rectangle by manipulating its Width property. The Rectangle control is placed behind a transparent Label control that defines the boundaries of the status meter and contains the status text. The status meter form is manipulated by three public wrapper functions contained in basStatusMeter: acbInitMeter, acbUpdateMeter, and acbCloseMeter. These functions, in turn, interact with frmStatusMeter through its exposed properties. The wrapper functions know the names of the properties and how to call them, but they know nothing of the inner workings of the form.
acbInitMeter initializes the status meter by opening the status meter form and calling the InitMeter method. At the same time, a parameter is passed that determines if the Cancel button is included on the status meter form:
DoCmd.OpenForm acbcMeterForm Forms(acbcMeterForm).InitMeter blnIncludeCancel, strTitle
acbUpdateMeter sets the value of the status meter form's UpdateMeter property. It then checks the Cancelled property of the form to determine whether the user has clicked on the Cancel button. If so, it closes the status meter form and returns False to the calling procedure; otherwise it returns True:
Forms(acbcMeterForm).Value = intValue ' Return value is False if cancelled. If Forms(acbcMeterForm).Cancelled Then Call acbCloseMeter acbUpdateMeter = False Else acbUpdateMeter = True End If
acbCloseMeter closes the status meter form using the DoCmd.Close method:
DoCmd.Close acForm, acbcMeterForm
Me.recStatus.Width = 0 Me.lblStatus.Caption = "0% complete" Me.Caption = strTitle Me.cmdCancel.Visible = blnIncludeCancel DoCmd.RepaintObject mblnCancel = False
This code sets the Width property of the recStatus control to 0 and the Caption property of lblStatus to "0% complete", updates the form's Caption property with the strTitle parameter, and sets the cmdCancel button's Visible property to match the blnIncludeCancel parameter. The code then uses the RepaintObject method to force an update of the screen and resets the mblnCancel module-level global variable to False.
When the UpdateMeter property of the form is set to a value, the following code is executed by the UpdateMeter procedure:
Me.recStatus.Width = CInt(Me.lblStatus.Width * (intValue / 100)) Me.lblStatus.Caption = Format$(intValue, "##") & "% complete" DoCmd.RepaintObject
This code updates the status meter by changing the width of the recStatus control relative to the width of the lblStatus control. This relative change ensures that the status meter rectangle never exceeds the limits as defined by the width of the lblStatus control. The routine then updates the Caption property of the lblStatus control to a formatted percentage value concatenated to the string "% complete". Once again, the code uses the RepaintObject method to force an update of the screen.
The Cancelled property of the status meter form is handled by the Cancelled Get property procedure. When called by an external procedure, this procedure returns the value of the module-level global mblnCancel variable. This variable, which was initialized to 0 by the IntitMeter Let property procedure, is set to False if the user clicks on the cmdCancel button in the cmdCancel_Click event procedure.
It's a good idea to encapsulate the inner workings of a generic utility form such as frmStatusMeter by keeping all the event procedures private and using procedures to expose a controlled user interface to calling procedures. Getting in the habit of thinking and coding in this object-oriented way will allow you to create generic components that you can reuse over and over again.
The pop-up status meter form's AutoCenter property has been set to Yes, so it will always appear in the center of the screen. You may wish to extend acbInitMeter with optional left and top parameters so you can precisely position the form on the screen when it is first opened.
|[ Team LiB ]|