VBA example - Microsoft Access: VBA two-dimensional array


VBA two-dimensional array

In this example, you will learn VBA two-dimensional array to store the coordinates for moving the picture. The picture is a flower that will be moved through coordinates into a vase.  The vase is constructed by  putting many image controls to together. To run VBA Code for moving the flower into the vase, you need a form as the figure below:

 VBA example - Microsoft Access VBA Array Two Dimensions Form 

-When the form loads, the vase is constructed and the flower picture moves step by step till reach the vase.

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

VBA example - Microsoft Access VBA Array Tow Dimensions Form Design 

Note: Unselect Use Control Wizards before you drag these controls to the form.

-Drag and drop the image control as many as you need  to construct the vase and leave one image control for the flower picture(rose).

- Set Name properties of  an image control that contains the moving flower(rose):

Name: Img0

 

Then apply the VBA Code below:

Option Compare Database

Option Explicit

Dim Cords(9, 1) As Integer

Dim n As Integer

 .................................................................................................

Private Sub Form_Load()

    ' Display a vase shape

    Dim i As Integer

    Dim cnt As Control

    i = 0

    For i = 0 To Controls.Count - 1

        If Left(Controls.Item(i).Name, 5) = "Image" Then

            Set cnt = Controls.Item(i)

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

            Img0.Picture = CurrentProject.Path & "\images040.jpg"

        End If

    Next

    ' Set coordinates for moving the flower picture

    Cords(0, 0) = 60

    Cords(0, 1) = 4140

    Cords(1, 0) = 150

    Cords(1, 1) = 4050

    Cords(2, 0) = 250

    Cords(2, 1) = 3950

    Cords(3, 0) = 350

    Cords(3, 1) = 3850

    Cords(4, 0) = 450

    Cords(4, 1) = 3750

    Cords(5, 0) = 550

    Cords(5, 1) = 3650

    Cords(6, 0) = 650

    Cords(6, 1) = 3550

    Cords(7, 0) = 750

    Cords(7, 1) = 3450

    Cords(8, 0) = 850

    Cords(8, 1) = 3350

    Cords(9, 0) = 990

    Cords(9, 1) = 3150

 

    n = 0

    Me.TimerInterval = 50

End Sub

 .................................................................................................

Private Sub Form_Timer()

' Move the flower(rose) through coordinate

     If n < 10 Then

        Call movepic(Cords(n, 1), Cords(n, 0))

        n = n + 1

    End If

End Sub

 .................................................................................................

' Move picture

Sub movepic(x As Integer, y As Integer)

    Img0.Left = x

    Img0.Top = y

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.