ItsMe,
I have had several Logger type functions over the years. I use Debug.Print a lot in development/debugging, but I had occassions to write text files for certain apps. Seemed reasonable to create one called Logger that would allow me "unlimited records", since Immediate Window Debug.Print is a fixed size that overwrites ( the subject of this thread).
Anyway, here is a simple logger function.
Code:
'---------------------------------------------------------------------------------------
' Procedure : Logger
' Author : jack
' Date : 1/21/2009
' Purpose : To write records to a LOG file using FileSystemObject.
'
'Parameters
' sLogName As String -- full path and file name of the log file
' sLogRec As String -- record to be written to the log
'
' NOTE: Each log record has a timestamp appended
'
' Special Note/restriction:
'***** Must set a reference to MICROSOFT SCRIPTING RUNTIME ***
'
'****The logfile must exist prior to calling this function *****
'---------------------------------------------------------------------------------------
'
Sub Logger(sLogName As String, sLogRec As String)
Dim tslog As TextStream
Dim fileLog As file
Dim i As Integer
Dim fso As FileSystemObject
On Error GoTo Logger_Error
Set fso = New FileSystemObject
Set fileLog = fso.GetFile(sLogName) '"I:\wordtest\output\Results.log")
Set tslog = fileLog.OpenAsTextStream(ForAppending)
tslog.WriteLine Now() & vbTab & sLogRec
tslog.Close
On Error GoTo 0
Exit Sub
Logger_Error:
MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Logger of Module ADO_Etc"
End Sub
Here is a small test routine showing
-identifying the logfile
-calling Logger
Code:
'---------------------------------------------------------------------------------------
' Procedure : testLogger
' Author : Jack
' Created : 4/18/2011
' Purpose : Sample procedure to use the Logger function
' Log file must exist before using Logger
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Sub testLogger()
Dim mlog As String
Dim res As String
On Error GoTo testLogger_Error
res = "*Pass "
mlog = "I:/wordtest/output/result_apr2011.txt"
Dim i As Integer
For i = 1 To 20
Call Logger(mlog, res & i)
Next i
On Error GoTo 0
Exit Sub
testLogger_Error:
MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure testLogger of Module ADO_Etc"
End Sub
I'm sure you've written similar, but thought I would respond to the post since I have seen others asking about writing a simple log.
I don't know if it's ANSI but carriage return (13), linefeed(10) were record/line terminators in the old days.