Tuesday, February 4, 2014

  
 VBA example - Microsoft Access VBA For Next Heart Form  
  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 above.
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

0 comments:

Post a Comment