Two Hoots Banner



Visual Basic Debug Tips

  • To step though code behind a certain screen element at run time, hit Ctrl-Break to break while the program is running, then press F8 to enter break mode just as the next line of code is about to be executed. Click the required screen element will put you into break mode in the code for that element.

  • VB strips out comments in the final EXE file, so use as many comments as you need.

  • Place the DEBUG.PRINT expression at strategic places in your code so the code will be written to the immediate window without disturbing the flow of the program. The DEBUG statement will be ignored when you build an EXE.

  • While in debug mode use the add instant watch dialog (Shift-F9) to show the value of the highlighted variable. This is much quicker than typing the expression in the debug window.

  • Don't forget to do a full compile (Ctrl-F5) to final all compile-time errors. Just running your program by using F5 doesn't guarantee error free code.

  • Use the Err object to get information about an error, or to create new errors for someone else to handle. When you assign an error number, use the vbObjectError constant. For example, see Err.Number to assign vbObjectError + 1000. This constant ensures that your error numbers won't conflict with Microsoft's. See below for the err objects Properties and Methods.
PropertiesDescription
NumberA unique number referencing an error.
SourceThe project name or class name if the error occurred in a class.
DescriptionA text description of the error.
HelpFileA fully qualified path to a help file.
HelpContentA context ID inside the help file containing help on this error.
LastDLLErrorAn error code produced by a call to a dynamic link library.


MethodsDescription.
ClearResets all the elements of the Err object.
Raise numberCreates a new error and sets the value of the Err object through the parameters.


  • Select Tools->Options->Editor->Require Variable Declaration to add an Option Explicit to all new modules. This forces you to declare all variables.

  • Select Tools->Options->Editor->Auto Syntax Check to compile each line of code as you leave it.

  • Use the Edit Toolbar Comment and UnComment commands to comment/uncomment blocks of code in a snap.

  • If you right-click on the right-most pane of the Object Browser, you can issue the Show Hidden Members command. From this point on, the Object Browser shows all hidden properties and methods in any library, and you can use it to explore all object libraries in more detail.

  • Visual Basic gives you the option to turn off error trapping in your projects. This is useful when you suspect that your error handlers contain errors themselves. To turn off error handling globally, choose Options from the Tools menu. Select the General tab, and select Break on All Errors. The next time you run your project in the development environment, Visual Basic will break whenever an errors in your code, whether the error is trapped or not.

  • Add the following code to email error messages to helpdesk :
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _ 
 (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
  ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Const SW_SHOWNORMAL As Long = 1
Public Const SYSTEM_ROOT As String = "C:\"

Private Sub SendErrorReport(ByVal istrErrorMessage As String)

' Replace CRLF with %0d token to give proper line breaks
istrErrorMessage = Replace(instrErrormessage, vbCrLf, "%0d")

ShellExecute Form1.hwnd, "open", "mailto:company.support@company.com" & _
  "&subject=Application Error" & "?body=" & instrErrormessage, "", _
  SYSTEM_ROOT, SW_SHOWNORMAL

End Sub