X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Closed TopicStart new topic
> Debugging Techniques, Any Version    
post Dec 20 2009, 10:39 PM

Utter A-fishin'-ado
Posts: 17,958
Joined: 1-August 05
From: Cincinnati, Ohio, USA . . . ><((((°>

It’s a terrible feeling. You work for hours trying to get the code just right ... and when you run the code, you receive an error. What went wrong?
In this article we’ll discuss a number of ways to debug your code and get you back on track developing your app.
METHOD 1: Option Explicit
Perhaps one of the easiest ways to prevent errors is to catch them before we even run the code. “Option Explicit” does this for us. Adding these two words at the top of every code module (Forms, Reports, and Modules) will force us to declare our variables. That is a very good thing. We avoid misspelled variables, variable used out of scope, even misspelled controls. Here’s where to insert the code:

So, now that we’ve inserted the Option Explicit statement, how do we find out if our code has any issues? We need to Compile our code. From the VBA Editor, choose Debug> Compile from the toolbar.

If an error is found, an error message appears and the problem code is highlighted in the VBA Editor.

To make things even simpler, there is an option we can turn on to always insert this code for us. It’s found in the VBA Editor: Tools> Options> Require Variable Declaration

With this option turned on, all new code objects we create will have this statement inserted automatically. But if we have existing objects, we will have to go back through them one at a time and insert the statement manually.
METHOD 2: Debug.Print
The majority of the time, problems in code can be attributed to a declared variable not having the value we expect it to have. Sometimes all we need to know is what a variable holds as our code is running. One simple way to do this is to use "Debug.Print". This statement will print out the value of variables to the Immediate window during code execution. The Immediate Window is found at the bottom of the VBA Editor.

This method can be particularly useful in troubleshooting SQL statements.

You can even Cut text from the Immediate Window and, for instance, Paste it into a query in SQL view. Many times this will give you a more specific error message than from just running the code in VBA.
METHOD 3: Breakpoints (A.K.A. Code Stops)
I did you know you can actually stop code, look at some variables, and start the code where it left off? This can be particularly useful for complex looping routines where many variables are changing often. To place a breakpoint in your code, click the grey area right in front of the line of code you want to stop on.

Run your procedure as you normally would, and when the code hits the breakpoint, the VBA editor will appear with your code highlighted.

You can use as many as you want. To turn them back off, just click the small maroon circle and the breakpoint will be removed.
You can “step through” the code by hitting the F8 key and it will execute line by line through the entire procedure. To resume normal execution, just hit the F5 key and the code will pick up from the point you left off.
You can change the value of variables during code execution to test out different values. This is done by assigning the variable a new value from the Immediate window.

You can even move the point of execution backwards or forwards.

This should not be considered an exhaustive list of methods for debugging code. Many other techniques are available.
These three tips may sound simple, but they are the mainstay of good developers. Taken advantage of, they can save you hours of frustration while debugging your code. Hopefully you will find them to be a useful part of your development strategy.
I would like to thank the many members of the UtterAccess community who assisted with the development of this FAQ article.
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    7th August 2020 - 07:32 PM