Implement a low-tech trace of your application by writing a message whenever your code enters and exits a subroutine. This can help you locate elusive bugs, because you can turn on the trace and run your application until failure. The last entry in your log indicates what subroutine you were executing when the failure occurred. Enter this code in the General Declarations section of a module:
Public DebugMode as Boolean
Normally, you should set this variable to False. During debugging, however, set it to True (DebugMode = True). Place this sub in the module:
Public Sub WriteDebugMessage(ByVal Msg as String) If DebugMode then 'write a message, including the system time End If End Sub
Enter this code as the first statement in every subroutine and function in your project:
Call WriteDebugMessage("Entering NameOfSubroutine")
Enter this code as the last statement in every subroutine and function:
Call WriteDebugMessage("Exiting NameOfSubroutine")
VB4 introduced conditional compilation, which offers a simpler way to implement this scheme. By defining a conditional compiler constant rather than a global flag variable, you can optimize your final EXE by toggling the constant to False. Conditional calls to the WriteDebugMessage routine won’t be compiled into your shipping build:
#Const DebugMode = False' for final code, true to test#If DebugMode Then Call WriteDebugMessage("Important Info")#End If
Use this code to write other debug information to the log, such as variable values. Don’t forget to turn off Debug mode when you’re not using it to avoid writing massive amounts of unneeded data to the log.