Hi Ed
Create a log file or log document even. Here's a class module that will look
after logging for you. Add a new Class module to your project and call it
"LogIt", then paste in the following code:
<====================Start of class LogIt======================>
Private mstrFile As String
Private mstrPath As String
Private mboolEnabled As Boolean
Private mboolTimeStamp As Boolean
Public Sub Output(ByVal strText As String)
Const cProcedureName As String = "Log::Output"
Dim hFile As Long
On Error GoTo OutputError
' Everything must be right for us to log the passed text
If CanOutput Then
' Prefix log text with timestamp
If TimeStamp Then strText = Now & vbTab & strText
' Open log file for append
hFile = FreeFile
Open mstrPath & mstrFile For Append Access Write As hFile
' Write the output and tidy up
Print #hFile, strText
Close hFile
End If
OutputExit:
Exit Sub
OutputError:
mboolEnabled = False
Err.Raise Err.Number, cProcedureName, Err.Description
End Sub ' Output
Public Sub Kill()
On Error GoTo HandleError
' Delete the current log file if it exists
If Len(Dir$(mstrPath & mstrFile)) > 0 Then
Kill mstrPath & mstrFile
End If
ExitHere:
Exit Sub
HandleError:
mboolEnabled = False
Err.Raise vbObjectError + 8001, "Log::Kill", _
Err.Description
Resume ExitHere
End Sub ' Reset
Public Property Get Path() As String
Path = mstrPath
End Property
Public Property Let Path(ByVal strPath As String)
strPath = Trim$(strPath)
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
mstrPath = Trim$(strPath)
End Property
Public Property Get File() As String
File = mstrFile
End Property
Public Property Let File(ByVal FileName As String)
mstrFile = FileName
End Property
Public Property Get Enabled() As Boolean
Enabled = mboolEnabled
End Property
Public Property Let Enabled(ByVal EnableLogFile As Boolean)
mboolEnabled = EnableLogFile
End Property
Public Property Get TimeStamp() As Boolean
TimeStamp = mboolTimeStamp
End Property
Public Property Let TimeStamp(ByVal TimeStampOutput As Boolean)
mboolTimeStamp = TimeStampOutput
End Property
Public Property Get CanOutput() As Boolean
CanOutput = LenB(Path) > 0 And LenB(File) > 0 And Enabled
End Property
Private Sub Class_Initialize()
Me.Enabled = True
End Sub
<=====================End of class LogIt=======================>
To use it you need to instantiate it and you're away:
Dim logFile As LogIt
' Create log file
Set logFile = New LogIt
logFile.Path = "F:\My Templates\Test Documents\"
logFile.File = "DEBUG ListTemplate.txt"
' Use it....
logFile.Output "This is the first log entry"
logFile.Output "This is the second log entry "
The advantage of using classes is that the code is fully encapsulated and you
never need to look at it. Oh, and of course you can have as many log files at a
time as you want!!
HTH + Cheers - Peter