Microsoft Access VBA Macro Programming

(Tina Sui) #1

Bear in mind that when this code runs, it will keep looping continuously forever until you
pressCTRL+BREAK,or until you write code within the loop to stop it after a set period of
time, or some event happens, such as the form is closed.
The code is attached to the Form Open event so it is fired off as soon as the form is
opened. Variables are set up to hold a horizontal increment value (HIncr), a vertical
increment value (VIncr), and a speed value (Spd) to determine how fast the label will move
about. By using this in the code as both a positive and negative value, it will also determine
the direction of movement.
HIncr and Vincr are initially set to the value of Spd. This is followed by a looping process
that will only end when 1 = 2, which will never happen, of course. A different condition can
be used here so that the animation only runs for a set period of time.
The code then checks if the label box has reached a border of the form (top, bottom, left,
or right). If it has reached a border, then the code changes the value of HIncr or VIncr, as
appropriate, to reverse the direction of the label box.
Notice that the Me object is used as a representation of the current form.
For example, in the firstIfstatement, if the left property plus the horizontal increment
value are less than 0, then the label box has reached the left-hand side of the form and so the
horizontal increment needs to change to a positive value to make the label box move from left
to right.
Notice that the properties ofInsideWidthandInsideHeightare used to determine the
bottom and right limits of the form. The width and height properties of the label box are also
used in the calculation. This is to prevent the label box from disappearing momentarily when
it gets to the bottom of the form or the border of its right-hand side.
For example, in the secondIfstatement, if theLeftproperty of the label box is greater
than the inside width of the form, less the width of the label box, then the label box has
reached the right-hand side of the form and the horizontal increment needs to change to a
negative value to make it move from right to left.
Now that the new values of HIncr and VIncr have been calculated, theLeftandTo p
properties of your label box are incremented by these values. Finally, the form is repainted so
that the changes in the label box properties take effect.
To run the code, all you need to do is click Design in the Access menu bar and then click
the Form View icon in the Views group of the ribbon. Your text box will be seen bouncing
around your form until you pressCTRL+BREAKor the form is closed.
You can easily use this as a splash screen for your Access application or as a window to
display while a lengthy query or process is running. You can also substitute a picture control
for the label box to use a graphic image.


Chapter 22: Animation 275

Free download pdf