Learn Access 2003 VBA with The Smart Method
74
www.LearnAccessVBA.com
Lesson 4-2: Step through code
Whenever we write code we’re likely to make mistakes. Programmers
call mistakes bugs for interesting historical reasons (see sidebar).
Now is a great time to introduce one of the VBA Debug tools. The debug
tools help you to track down the source of any problems that are
encountered when you test your code.
There are many debug tools and we’ll be learning about all of them in
future lessons. This lesson introduces one of the most useful tools: the
ability to step through code.
1
Open the VBACode.mdb database created in the last
chapter (if not already open).
2
Open the event handler for the cmdPressMe button’s Click
event.
The Code Editor window is displayed.
3
Enable the Debug toolbar if it is not already displayed.
You can do this by selecting View Toolbars Debug from the
main menu.
4
Add a breakpoint on the line
Call ThankYouMessage
.
Click anywhere in the text Call ThankYouMessage and then click the
Toggle Breakpoint button
on the Debug toolbar to insert a
breakpoint. The selected text is highlighted.
Try clicking the Toggle Breakpoint button
twice more to
remove and add the breakpoint.
Try clicking in the margin (where you see the brown circle) and
note that this method can also be used to toggle breakpoints.
5
Return to Access by clicking the View Access button
on
the Code Editor’s standard toolbar and then change frmTest
from
Design View
to
Form View
.
6
Click the cmdPressMe command button.
You are transferred to the code editor with code execution halted
at the breakpoint.
note
The reason that errors in
computer code are called bugs
is often said to date from an
incident at Harvard University
in 1945.
There was a problem with a
very early mainframe computer
when a moth became trapped
in the circuits. From then on,
when anything went wrong
with it the programmers would
blame the bugs.
While this story is undoubtedly
true the reality is that the first
recorded use of the word in this
context is in the following
quote from the Pall Mall Gazette
of 11 March 1889:
“Mr. Edison, I was informed, had
been up the two previous nights
discovering 'a bug' in his
phonograph - an expression for
solving a difficulty, and implying
that some imaginary insect has
secreted itself inside and is causing
all the trouble".
Session4a
pg_0002
Session Four: An Introduction To VBA
© 2007 The Smart Method Ltd
75
7
Click the Step Into button
on the Debug Toolbar twice.
The code steps into the ThankYouMessage sub procedure header
and then to the line saying Beep.
8
Click the Step Into button
on the Debug Toolbar once
more
An audible Beep is heard as the code executes and the code steps
to the next line Call MsgBox…
9
Click the Step Into button
on the Debug Toolbar once
more
The message box is displayed. Clicking the OK button on the
message box steps the code to the next line End Sub.
10
Click the Step Into button
on the Debug Toolbar once
more
The code moves to the End Sub statement from the calling function
cmdPressMe_Click.
11
Click the Step Into button
on the Debug Toolbar once
more
Nothing happens as all code has executed.
12
Remove the breakpoint.