Debug code to text file

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
Is there a way to have any messages that are normally sent to the immediate window sent to a text file that I can view. This way if there is an error I can use it to help debug from other users of my application.

(MS ACCESS 2010)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I have some code for a routine I call Logger that write a timestamped message to a file.
You Identify the file.

Code:
' 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 ***
'---------------------------------------------------------------------------------------
'
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


Typical usage

Code:
....
Dim myLog As String
myLog = "I:\jColby\myLog.txt"
Dim x As String

Set db = CurrentDb()
I = 0    'for testing
hName = "zzzqqq"
x = "List Tables with Indexes and/or " & vbCrLf & "Primary Key and Display Components" & vbCrLf & vbCrLf
Debug.Print x
Logger myLog, x
....


I just saw Norie's post. I'm assuming you're saying messages , but are referring to
Debug.Print to the immediate window.
 
Last edited:
Upvote 0
How are the messages being sent to the Immediate window?

That's not any built-in option so it must be getting done by some other code.

Is that what's happening?
 
Upvote 0
What jackd just posted is exactly the kind of thing I meant.

A separate sub that writes the the error message and any other relevant information to a file.
 
Upvote 0
Ok I will give this a try. I have to work on this this weekend or next week if I get a chance.
 
Upvote 0
I might have to ask a few more questions when I am testing this out.

Thanks all.
 
Upvote 0
You can do it without the filesystem dialog as well:

Code:
Function writeToLog(strLogFile As String, strDebug As String)
   Open strLogFile For Append As #1
 
    Print #1, strDebug
 
   Close #1  
End Function

Then you can write to it by using
Code:
writeToLog "C:\Temp\MyLog.log", x

or you could use a table for the log name and use a DLookup instead of a parameter passed to it or you could hard code it. But very few lines of code really needed.
 
Upvote 0
In regards to the code:

Do I have to set the file name twice? Or can I just set it in the Logger Sub and everytime to the file?

I have some code for a routine I call Logger that write a timestamped message to a file.
You Identify the file.

Code:
' 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 ***
'---------------------------------------------------------------------------------------
'
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


Typical usage

Code:
....
Dim myLog As String
myLog = "I:\jColby\myLog.txt"
Dim x As String

Set db = CurrentDb()
I = 0    'for testing
hName = "zzzqqq"
x = "List Tables with Indexes and/or " & vbCrLf & "Primary Key and Display Components" & vbCrLf & vbCrLf
Debug.Print x
Logger myLog, x
....


I just saw Norie's post. I'm assuming you're saying messages , but are referring to
Debug.Print to the immediate window.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,245
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top