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)
 
Bob you are the best! Thank you that was so simple. I just hard coded the log file name into the function to save on typing as I only need one log file for it.
P.S. Also thank you for your Front End Auto Update Tool... I have added that to our project!

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

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Personally, I would go with the much shorter code I posted. I just don't see any benefits from the longer, more complicated code, do you?
 
Upvote 0
If you want to hard code the log file name in - you could use:
Code:
Function writeToLog(strDebug As String)
   Open "C:\Temp\MyLog.log" For Append As #1
 
    Print #1, strDebug
 
   Close #1  
End Function

And then just call this code like you would the Debug.Print code
 
Upvote 0
Personally, I would go with the much shorter code I posted. I just don't see any benefits from the longer, more complicated code, do you?

The longer code had given me an error, just as I was going to mention that I saw your code and it was great! It also appears to act just like Debug.print feature. It gives me the exact output I need.
 
Upvote 0
I actually went with the following to make it easier for me to edit the file name without risk of screwing up the actual command.

Code:
Function writeToLog(strDebug As String)
   
   Dim strLogFile As String
   strLogFile = "T:\SNRTool\Logs\Processing.log"
   
   Open strLogFile For Append As #1
 
    Print #1, strDebug
 
   Close #1
End Function

If you want to hard code the log file name in - you could use:
Code:
Function writeToLog(strDebug As String)
   Open "C:\Temp\MyLog.log" For Append As #1
 
    Print #1, strDebug
 
   Close #1  
End Function

And then just call this code like you would the Debug.Print code
 
Upvote 0
What I would really suggest is to have a table with the path and name for the log file and then use a Dlookup to get it when necessary. That way you don't have to revise any code if you want the log file path or name to change.

So, for example, have a table named tluPreferences and have these fields:

PrefID - Autonumber (PK)
PrefName - Text
PrefValue - Text

And then you can have the PrefName text for this set to ErrorLog and then the PrefValue text to the file path and name (like C:\Temp\MyLog.log)

And then in the code you change to:

Code:
Function writeToLog(strDebug As String)
   
   Dim strLogFile As String
   strLogFile = Nz(DLookup("PrefValue", "tluPreferences", "[PrefName]='ErrorLog'"), vbNullString)
   If strLogFile <> vbNullString Then   
      Open strLogFile For Append As #1
 
         Print #1, strDebug
 
      Close #1
   Else
      MsgBox "No log file has been specified in tluPreferences", vbExclamation, "Missing info"
   End If
End Function
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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