Animating Microsoft Office Access 2003
Frank Rice
Microsoft Corporation
January 2004
Applies to:
Microsoft® Office Access 2003
Summary: Learn different methods of animating Microsoft Office Access 2003. These include using the Animation ActiveX control, the Timer control, and animating the Office assistant. (7 printed pages)
Download ODC_acanimateSample.exe.
Contents
Introduction
Animating Access with the Animation ActiveX Control
Animating the Office Assistant
Animating with the Timer Object
Conclusion
Introduction
Displaying bitmaps and other shapes in Microsoft® Office Access 2003 forms and reports adds visual interest. However, to add dazzling effects to your applications, animation is your best bet. Animation is the simulation of movement by using a rapidly displayed series of related images on the screen. Real automation involves moving objects programmatically to produce the affect. In this article, you will learn different ways to add simple automation to your Access forms. The files described in this article are also included in the download that is available from the Microsoft Download Center.
Animating Access with the Animation ActiveX Control
With the Animation ActiveX® Control, you can play .avi files on a Microsoft Access form. When you open the form in Design view, you can see the control; however, in Form view, the control is hidden until you play the .avi file associated with the control. One common use for the animation control is to provide animation with command buttons. If you place the animation control on top of a command button and use the OnClick event of the button to invoke the Open and Play methods of the animation control, the button appears to be animated. The Animation ActiveX Control supports only two types of .avi files: uncompressed .avi files, and .avi files that are compressed using Run-Length Encoding (RLE).
Note .RLE files store pixel color by run length, or the number of similar colored pixels that are side by side; whereas .BMP files store the color values for each pixel in the bitmap. A line in a .BMP file may look as follows:
blue, blue, blue, red, red, white, white, white, white
The line above would appear as follows in an .RLE file:
3 blue, 2 red, 4 white
Sound components of .avi files are not played.
The Microsoft Animation Control is included in the Microsoft Windows® Common Controls-2 6.0 that is available from the following location:
Example 1: Using the Animation Control to Animate a Command Button
Start Microsoft Access and create a blank database called Animate1.mdb.
Create a form not based on any table or query in Design view.
On the Insert menu, click ActiveX Control.
In the Insert ActiveX Control dialog box, select Microsoft Animation Control, version 6.0, and then click OK.
Set the following properties for the animation control:
- Name: an1
- Visible: No
- Left: 1"
- Top: .5"
- Width: 1"
- Height: 1"
Add a command button to the form, and set the following properties:
- Name: cmd1
- Caption: Play Animation
- Left: 1"
- Top: .5"
- Width: 1"
- Height: 1"
Type the following code in the command button's OnClick event procedure:
Private Sub cmd1_Click() Dim AviFile As String 'Type the full path to the .avi file on the following line. AviFile = path and filename With an1 .Visible = True .AutoPlay = True .Open AviFile End With End Sub
Note For this code to run properly, you must insert the correct path and filename of your .avi file. For this example, type the path for the file DOWNLOAD.AVI.
Save the form as frmAnimate, and then switch to Form view.
Click the command button and note that the .avi file plays on top of the command button.
Example 2: Selecting and Playing an Animation File
Follow steps 1 through 3 in Example 1.
On the Insert menu, click ActiveX Control.
In the Insert ActiveX Control dialog box, select Microsoft Animation Control, version 6.0, and then click OK.
Set the Name property of the animation control to An2.
On the Insert menu, click ActiveX Control.
In the Insert ActiveX Control dialog box, select Microsoft Common Dialog Control 6.0, and then click OK.
Note The Microsoft Common Dialog Control 6.0 is included with Microsoft Office.
Set the Name property of the common dialog control to Cd2.
Add a command button to the form, and set the following properties:
- Name: cmd2
- Caption: Select and Play
Type the following code in the command button's OnClick event procedure:
Private Sub cmd2_Click() With Cd2 .Filter = "avi (*.avi)|*.avi" .ShowOpen End With With An2 .AutoPlay = True .Open Cd2.FileName End With End Sub
Save the form as frmSelectPlay, and then switch to Form view.
Click the command button.
Notice that an Open dialog box appears and lets you select an .avi file, and then the .avi file plays on the form.
Animating the Office Assistant
At times, you may want to animate the movements of the Office Assistant in response to actions performed in your application. For example, you may want to have the Assistant appear when you open a particular form. Or you may want the Assistant to react when a certain event is triggered. The following steps show you how to do this.
Note To activate the Office Assistant, on the Help menu, click Show the Office Assistant. If you receive a message that the Office Assistant is not installed, rerun the Office Setup program and choose Office Assistant from the Office Shared Features option.
Starting the Assistant:
Open the sample database Animate.mdb.
Open the Employees form in Design view.
Set the OnOpen event of the form to the following event procedure.
Note The following sample code requires that you have a reference to the Microsoft Office 11.0 Object Library in your database. To create the reference, open any module in Design view, click References on the Tools menu, and then click the Microsoft Office 11.0 Object Library.
Private Sub Form_Open(Cancel As Integer) With Assistant .Filename = "Clippit.acs" ' Returns or sets the name of ' the active Office Assistant. .Visible = True .Animation = msoAnimationGreeting ' Simulates greeting user. .Sounds = True .SearchWhenProgramming = True .FeatureTips = True End With End Sub
On the File menu, click Close and Return to Microsoft Access.
Changing the Movement of the Assistant:
Set the AfterUpdate property of the FirstName text box to the following event procedure:
Private Sub FirstName_AfterUpdate() With Assistant .Animation = msoAnimationListensToComputer ' Animates Assistant. End With End Sub
On the File menu, click Close and Return to Microsoft Access.
Closing the Assistant When the Form Closes:
Set the OnClose event of the form to the following event procedure:
Private Sub Form_Close() If Assistant.Visible = True Then With Assistant .AssistWithHelp = False .SearchWhenProgramming = False .GuessHelp = False .FeatureTips = False .Visible = False End With End If End Sub
On the File menu, click Close and Return to Microsoft Access.
Save the form, and then open it in Form view. Note that the Clippit Assistant is displayed.
Type a new name in the First Name field and press ENTER. Note that the Clippit Assistant responds with listening animation.
Close the form. Note that the Clippit Assistant closes.
Animating with the Timer Object
Another way to create animations is by using the Timer event so that at set intervals, an object is animated. When you create animations, you set the Timer Interval property of the timer to the rate that you want the animation to move. For example, in our sample, we set the Timer Interval property to 1000 milliseconds, which is equal to one second. Another technique is to use the Top and Left properties to sense the edge of the form. Using these properties allows you to act on the animation before the edge of the form is exceeded, thus causing an error. Finally, the Move method allows you to move the icon within the coordinate system.
The following demonstrates how you can animate a picture box control containing an icon (FACE04.ICO) by using a timer object. In this sample, the Left property detects the bottom edge of the form. The Top property sets the upper limit to which the icon moves.
Open the sample database Animate.mdb.
Create a new form and open it in Design view.
Open the Property box for the Detail section and set the Height property to at least 2.5 inches and the Width property of the form to at least 4 inches.
Using the Image control, draw a small rectangular picture box object approximately in the center of the form.
Navigate to the FACE04.icon and click OK.
Open the Property box for the form and set the Timer Interval property to 1000.
In the OnLoad event for the form, insert the following procedures:
Private Sub Form_Load() Me.TimerInterval = 1000 End Sub Public Sub Form_Timer() MoveHappy End Sub Public Sub MoveHappy() Dim oImage As Image Set oImage = Me.Image0 Beep If oImage.Top > 50 Then If oImage.Left > 50 Then oImage.Move oImage.Top + 1000, oImage.Left - 1000 End If End If End Sub
Close and save the form as frmMovingHappyFace.
Open the form in Form view. Notice that the icon appears to bounce.
Conclusion
In this article, we looked at different methods of adding animations to your Access forms. These included using the Animation ActiveX control, the Timer control, and animating the Office assistant. Adding animation to the objects in your Access applications can help make your forms look more professional and add visual effects that draw the attention of your customer.