Access Cookbook - Ken Getz [192]
This solution is designed to be generic. You may find that you need a more specific function for a particular combo box. For example, you could allow users to cancel out of the pop-up form in case they decide against adding a new record, or you could use unbound text boxes on the data entry form to display pertinent information from the main form, adding context for data entry.
See Also
See Recipe 7.5 in Chapter 7 for more information on working with list and combo boxes.
9.7. Create Animated Buttons
Problem
You'd like to add some pizzazz to your application. You've seen animated buttons in other applications; how do you create them on your forms?
Solution
Access command buttons have an under-documented property called PictureData that stores the bitmap displayed on the button face. This solution examines two ways to use this property. First, you will learn how to create "two-state" buttons with pictures that change when you click on them. Next, you will learn how to create continuously animated buttons that cycle through a set of pictures at all times, using the form's Timer event to display a smooth succession of bitmaps.
Load 09-07.MDB and open frmAnimateDemo in form view (Figure 9-20). The top two buttons are two-state buttons whose pictures change when you click them. The Copy button (on the top left) shows a second document, and the Exit button (on the top right) shows the door closing just before it closes the form. The bottom two buttons are examples of animated button faces. (Only the Exit button on this form actually does anything when you press it.)
Figure 9-20. The frmAnimateDemo form
Two-state buttons
To add a two-state animated button to your form, follow these steps:
Open your form in design view. Place a pair of command buttons on the form. The first button should be sized correctly for your pictures and be located where you want the button to be displayed. The second button can be located anywhere and can be any size. For example, the two-state command button in the top left corner of frmAnimateDemo was created with cmdCopy and cmdCopy2. The cmdCopy button is shown selected in design view in Figure 9-21; cmdCopy2, which has been reduced in size to save space, is located just to the left of cmdCopy. Set the Visible property of the second command button to No.
Figure 9-21. The frmAnimateDemo form in design view
Click on the first command button of the pair, select the Picture property on its properties sheet, and click the Build button (...) to the right of the property. When the Picture Builder Wizard appears, select the face you want your button to have in its unselected state (see Figure 9-22). You can use the Browse button to choose from bitmap files on your disk.
Figure 9-22. The Picture Builder Wizard
Click on the second command button of the pair, select the Picture property, and load the face you want your button to have when it is depressed, again using the Build button.
Create an event procedure attached to the MouseDown event of the first button. (If you're unsure of how to do this, see Section P.5.5 in the the preface of this book.) Add the following code to the event procedure:
Private Sub cmdCopy_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Call SwapPictures(Me.cmdCopy, Me.cmdCopy2)
End Sub
Replace cmdCopy and cmdCopy2 with the names of your buttons.
Create the following event procedure attached to the MouseUp property of the first button:
Private Sub cmdCopy_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Call SwapPictures(Me.cmdCopy, Me.cmdCopy2)
End Sub
Again, replace cmdCopy and cmdCopy2 with the names of your buttons.
Add the following subprocedure to the form's module:
Private Sub SwapPictures(cmdButton1 As CommandButton, _
cmdButton2 As CommandButton)
Dim varTemp As Variant
varTemp = cmdButton1.PictureData
cmdButton1.PictureData = cmdButton2.PictureData
cmdButton2.PictureData = varTemp
Me.Repaint
End Sub