Access VBA Macro Programming

(Joao Candeias) #1
You can change the caption properties on aUserFormobject using code. This assumes you
have already definedUserForm1and that it is open in View mode. See Chapter 9 for how to
create a user form for use with VBA.

Sub test_for()
For n = 1 To 4

For m = 2 To 8

Forms("UserForm1").Caption = n & " xxxx " & m
Next m

Next n
End sub

This will display the variables in the caption of the window as the program is running.
Depending on how fast the variables change and how long the procedure is, you will be able
to see patterns occurring and see what is happening live at each stage. In terms of patterns,
you will be able to see sequences of numbers, how they ascend and descend, what the
maximum values they get to are, and so on. If a major error occurs, you can see at what value
it is occurring.

Avoiding Bugs.


Careful design and planning of the application is important and will help reduce bugs. Often
it is best to break the application down into smaller component parts that are easier to code
and test. It is easier to think of a small portion of code than to try to tackle an entire project in
one thought. Make sure you document and comment your code using the single quote (')
character. The comments will turn green within your code. If the application is complicated,
it is often difficult to go back even after just a few days to determine what the code is doing.
Even professional programmers find that if they go back to code they wrote only a few
months before, they have difficulty understanding what the code was doing. Comments may
provide the help needed. Also, if a programmer leaves an organization, it can be difficult for
a new or different programmer to pick up where the former employee left off without any
documentation about the intent of the code.
It is important to know what all your variables represent and what each function does.
Once your application starts growing and begins to get complicated, its documentation
becomes more important. Without such documentation, you will need a good memory to
keep track of what every variable means!

90 Microsoft Access 2010 VBA Macro Programming

Free download pdf