Access VBA Macro Programming

(Joao Candeias) #1

Got Focus / Lost Focus


A got focus / lost focus event occurs when a user clicks your form or a particular control on
the form to get the focus on that control. If you break the code at this point, you may get
inconsistent results. Whether you do or not depends on if your control had the focus at the
point that theBREAKkey was pressed.


Using Message Boxes in Debugging


Some methods besides those already discussed can isolate bugs in code. They may be
considered somewhat crude, but they do work. For example, message boxes are always useful
for letting you know what is going on. They can display the value of a variable or even
several variables by concatenating them together. They are extremely useful when you have a
large procedure and you do not know the region the bug is in. For example, a large piece of
code appears to run but hangs and will not respond toCTRL+BREAK. Perhaps it ran perfectly
up to now but has hit certain circumstances that cause it to hang.
The question is, how far into the code do you get before the issue occurs? You could try
stepping through it, but this is very time-consuming if it is a large procedure. Placing
message boxes at strategic points throughout the code will give you an idea of where it stops.
Make sure all the message boxes display something meaningful, such as “Ok1,” “Ok2,” and
“Ok3.” You can then see what stages the program has covered before it hangs. Be sure you
clear all the extra message boxes out once you have found the bug! You can also concatenate
variables together.


Sub test_for()
For n = 1 To 4
For m = 2 To 8


MsgBox m & " xxxxx " & n

Next m

Next n


End Sub


This message box will display the values ofmandnseparated by a line of x’s. The purpose
of the x’s is to prevent null values or spaces from being hidden. Although it would not happen
in this example, variables can sometimes end up with Null values or be spaces if they are
string variables, in which case you would only see one number appearing. In such instances,
how would you know whether it is the value ofmorn? The row of x’s distinguishes this.
If you want a continuous readout of variables while the program is running, there are ways
to get one. Debug does not offer this option, but with a little ingenuity it can be accomplished.


Chapter 7: Debugging 89

Free download pdf