VBA example - Microsoft Access: Loop to animation


VBA Loop creating animation

In this example, you will learn VBA For... Next Loop Statement to make pictures animate according to the timer. The example illustrates a form that has the pictures arranged as a heart like the figure below:

 VBA example - Microsoft Access VBA For Next Heart Form  

When the form is loaded and the timer runs, all the pictures will arrange as heart  shape(like the figure above) again and again till you close the form or the timer is stopped. The heart picture will appear in the middle of the form.

To have a form as the figure above, you must have a picture and create the form in Form Design. See as the figure below:

VBA example - Microsoft Access VBA For Next Heart Form Design 

- Drag and drop image controls as many as you need to arrange them in heart shape.

Note: Unselect Use Control Wizards before you drag the image control to the form.

Then apply the VBA Code below:

 

Option Explicit

Option Compare Database

Private Sub Form_Load()

TimerInterval = 100

DoCmd.Maximize

Dim cnt As Control

Dim i As Integer

Dim L As Integer

Dim T As Integer

For i = 0 To Controls.Count - 1

Set cnt = Controls.Item(i)

L = cnt.Left

T = cnt.Top

cnt.Move L + 4500, T + 1500

Next

End Sub

 

Private Sub Form_Timer()

' Display  animate heart

Dim i As Integer

i = 0

For i = 0 To Controls.Count - 1

  Dim X As Control

  Set X = Me.Controls.Item(i)

  ' Set flower1 picture to all image controls

   X.Picture = CurrentProject.Path & "\flower1.jpg"

Next

End Sub



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.