Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to debug your code

Error Resolution

How to debug your code

by  dhookom  Posted    (Edited  )
Few if any of us write code that is accurate and logical 100% of the time.

Always attempt to compile your code after every change. This is accomplished by selecting Debug->Compile [name of module]. Logic errors will not be detected however misspelled memory variables or other syntax errors should be caught.

It is also important to include Option Explicit at the top of every code module:
[code VBA]Option Compare Database
Option Explicit[/code]

If your code doesn't work as expected, you need to be able to debug it. For instance, if one of the early lines of your code is:
Code:
   stQueryName = Me.[cmbQueryName]
and it doesn't seem to be working correctly, you have at least three options for trouble-shooting.

Adding a breakpoint is as simple as clicking in the vertical bar to the left of a line of executable code.
Code:
|[red][b]o[/b][/red]|  [color white red][b]stQueryName = Me.[cmbQueryName][/b][/color]
The code will stop running and allow you to hover the mouse over a variable to see its value. You can press [F8] to step through the code a line at a time. [F5] will run to the end of the procedure.

The following will put the value in the debug window:
Code:
   stQueryName = Me.[cmbQueryName]
   Debug.Print "stQueryName: " & stQueryName
You can press Ctrl+G to open the debug window to view the results of the statement.

The following will stop the code execution and display the value in a message box:
Code:
   stQueryName = Me.[cmbQueryName]
   MsgBox "stQueryName: " & stQueryName
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top