In this example, you will learn the VBA conditional statement to make the animation of letters. The example shows the phrase Happy New Year!. When the form loaded and the timer run, the phrase Happy New Year! will start to show from one character to the end of this phrase. This example uses Timer event to make the letters animate. The figure below is the form to show animate letters.
To make the animate letters, you have to create a form as figure:
The form has only one label control. To create the form, you have to follow the steps:
- Create a form in Form Design
- Drag and drop a Label control on the form.
- Set caption property Label control:
Caption: Happy New Year!
The following VBA code below explicates the example.
Option Compare Database
Dim i As Integer
Dim ca As String
Private Sub Form_Load()
i = 0
ca = Me.lblhppnewyear.Caption
Me.TimerInterval = 700
lblhppnewyear.FontSize = 35
Private Sub Form_Timer()
If i < Len(ca) Then
i = i + 1
Me.lblhppnewyear.Caption = Left(ca, i)
Else: i = 0
' Assign multi color to the word Happy New Year! by randomizing the colors
Me.lblhppnewyear.ForeColor = RGB(50 + CInt(Rnd() * 200), 90 + CInt(Rnd() * 100), 0)
Note: Whenever you want to make something animate based on the computer timer, Timer event is used. Put the code to make something animate in the form's Timer event procedure and set TimerInterval in the form's Load event procedure. The Timer can run in a duration of time according to its TimerInterval property. If you set TimerInterval is small the animation run fast and vice versa. The unit of TimerInterval is millisecond; if you set 1000 to TimerInterval, it is equal to one second.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: