Option Compare Database
Option Explicit
Public Function writeLog(text As String)
'Logdatei leicht gemacht
'by c0bRa
'Variablendeklaration
Dim fs As Object, f As Object
Set fs = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
'Pfad erstellen, falls nicht vorhanden
MkDir Application.CurrentProject.Path & "\Log\"
On Error GoTo writeLognummer1
'Datei öffnen, wenn nicht vorhanden, erstellen
Set f = fs.OpenTextFile(Application.CurrentProject.Path & "\Log\" & _
Format(Date, "yyyy-MM-dd") & ".log", 8, -2)
GoTo writeLognummer2
writeLognummer1:
On Error GoTo writeLognummer2
Set f = fs.CreateTextFile(Application.CurrentProject.Path & "\Log\" & _
Format(Date, "yyyy-MM-dd") & ".log", True)
writeLognummer2:
On Error GoTo 0
If text = "" Then
f.Write vbCrLf
Else
f.Write "«" & Time & "» " & text & vbCrLf
End If
f.Close
End Function
This code creates a Folder called Log if it doesn't exist and creates a log file for every day. You can call the logging from within your code with this short command.
writeLog "Dies ist ein Testtext"The code is from a guy called c0bRa - thanks for that :)
No comments:
Post a Comment