Access Cookbook - Ken Getz [193]
Continuously animated buttons
To add a continuously animated button to your form, follow these steps:
From 09-07.MDB, import tblButtonAnimation, frmButtonFaceChooser, basAnimate, and basCommonFile into your own database.
Open frmButtonFaceChooser (Figure 9-23) and select eight images for use on your animated button. You can type the filenames directly into the text boxes, or click on the numbered buttons to select files from the common file dialog. The pictures will appear on the command buttons as you choose them. The buttons are sized for standard 32 32-pixel icons or bitmaps, but you may use images of any size.
Figure 9-23. Choosing animation bitmaps with frmButtonFaceChooser
When you have selected eight bitmaps, enter an animation name to refer to this set of pictures (for example, "clock") and click on the Save button.
Create a new blank form and place a command button on it. Set the form's properties as shown in Table 9-5.
Table 9-5. Property settings for animated button form
Property
Value
OnLoad
Event Procedure
OnTimer
Event Procedure
TimerInterval
250
Enter the following code in the declarations section of the form's module:
Private Const acbcImageCount = 8
Private mintI As Integer
Private abinAnimation1(1 To acbcImageCount) As Variant
Create the following event procedure attached to the form's Load event:
Private Sub Form_Load( )
Dim db As DAO.Database
Dim rstAnimation As DAO.Recordset
Dim intI As Integer
mintI = 0
Set db = CurrentDb( )
Set rstAnimation = db.OpenRecordset("tblButtonAnimation", _
dbOpenDynaset)
' Loop through the table, and load
' the animation images
With rstAnimation
.MoveFirst
.FindFirst "AnimationName='checkmark'"
For intI = LBound(abinAnimation1) To UBound(abinAnimation1)
abinAnimation1(intI) = .Fields("Face" & intI)
Next intI
.Close
End With
End Sub
Replace 'checkmark' with the animation name you used in Step 3.
Create the following event procedure attached to the form's Timer event:
Private Sub Form_Timer( )
' mintI is 0-based, but the arrays are 1-based, so add 1.
Me.cmdCheck.PictureData = abinAnimation(mintI + 1)
' Bump to the next value, wrapping around at acbcImages
' (8, in this example).
mintI = (mintI + 1) Mod acbcImages
End Sub
Replace cmdCheck with the name of the command button you created in Step 4.
Save the form and open it in form view. You should see your animation running on the face of the button.
Discussion
Access stores the picture displayed on a command button in the PictureData property. This property is a binary representation of the bitmap displayed and is read/write in all views. To store the bitmap elsewhere, there are three choices: you can store it on another button, in a variable of the Variant data type, or in a table field of the OLE Object data type.
In this solution, you use all three of these techniques. The two-state buttons work by storing the normal image on the button you can see and parking the second image in a small, invisible button. You can still read and write the PictureData property of an invisible button. When you click the visible button, its MouseDown event procedure is called, which swaps the pictures on the visible and invisible buttons. The MouseUp event code swaps the pictures again to return the original picture to the button face.
For continuously animated buttons, the eight different button faces are stored in a table as Long Binary Data (this is what Access tells you if you open the table in datasheet view) in OLE Object fields. The form's Load event procedure reads these button faces into an array of variants, and its Timer event is used to fetch the next button face every 250 milliseconds in round-robin fashion.
In frmButtonFaceChooser, you'll find an easy way to load bitmaps into the tblButtonAnimation table. You can load a button's PictureData property by setting its Picture property to the name of any bitmap or icon file. The command buttons on this form use the Windows API common dialog functions to invoke the