Data Management

Keep a VB6 application log

Learn how to create an application log file, which Peter Aitken believes is the easiest way to troubleshoot user problems. Even though it requires a little more effort to program log file capability in a program, it can save you a lot of time and headaches down the road.

When you create an application that will be distributed to multiple users, it's a pretty sure bet that they'll be calling you with problems.

In order for you to diagnose the problem, it can be very useful to have a record of what exactly the program has been doing. The easiest way to do this is to maintain a log file of program activity. The user can send this file to you and by examining it you may well be able to locate the problem.

The first step is to add a procedure to your project that writes to the file. I like to write the date and time as well as whatever information is relevant at the time. Here's an example:

Public Sub WriteToLogFile(logentry As String)

Dim fn As Integer
fn = FreeFile
Open App.Path & "\ProgramNameLog.txt" For Append As #fn
Write #fn, Now & ": " & logentry
Close #fn

End Sub

When called and passed a message, this procedure opens the specified file in the application directory, or it creates it if it doesn't exist. Then the procedure writes the current date and time and the information to the file and closes the file.

The next step is to call the procedure at the appropriate locations in your code. Where this will be depends on your program, but some examples of information that may be useful in a log file include:

  • Names of procedures that are called
  • SQL statements passed to a database
  • Names of files opened or created
  • Values of intermediate values in calculations
  • Data input by the user

I like to have the log file as an option that the user can turn on or off, for example, with a Checkbox in an Options dialog box. Set a global Boolean variable according to the option setting and then write to the log file only when the option is selected. For example:

If Logfile Then WriteToLogFile(sqlStatement)

It's a little more effort to program log file capability in a program, but it may save a lot of time and headaches down the road.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

0 comments