Tuesday, February 4, 2014


VBA example - Microsoft Access If Else Statement Letter Animation VBA
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 above is the form to show animate letters.
 To make the animate letters, you have to create a form as figure:

VBA example - Microsoft Access If Else Statement Design Form

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 Explicit
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
End Sub

Private Sub Form_Timer()
If i < Len(ca) Then
i = i + 1
Me.lblhppnewyear.Caption = Left(ca, i)
Else: i = 0
End If
' 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)

End Sub

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.

0 comments:

Post a Comment