Error Loggin Problem - Error 70 on the error log

5 views
Skip to first unread message

AC Me

unread,
Apr 29, 2008, 5:25:39 AM4/29/08
to Visual Basic Help Center
Hi All.

I have created an app to which I added an error logging capability.
Should an error occur information about that error is logged to a file
called Error.csv. Fine so far. But if the error log itself is open
when another error occurs then I get a run-time error 70 - permission
denied. I don't understand this.
Here is the error log function code

Public Function LogError(strError) As Integer

^ Dim fso As New FileSystemObject
^ Dim fl As File
^ Dim ts As TextStream

^ Set fso = CreateObject("Scripting.FileSystemObject")

^ On Error GoTo ErrorHandler

^ If (Dir("C:\TargetDir\Error.csv") = "" Then
^^ '//Create file as it does not exist
^^ fso.CreateTextFile("C:\TargetDir\Error.csv")
^^ Set fl = fso.GetFile("C:\TargetDir\Error.csv")
^^ Set ts = fl.OpenAsTextStream(ForWriting)
^^ '//Log the error string
^^ ts.WriteLine
^^ ts.Close
^ Else
^^ '//File already exists - open the file
^^ Set fl = fso.GetFile("C:\TargetDir\Error.csv")
^^ Set ts = fl.OpenAsTextStream(ForAppending)
^^ '//Add the error string
^^ ts.WriteLine(strError)
^^ ts.Close
^ End If

^ LogError = 1
^ Exit Function

ErrorHandler:
^ LogError = Err.Number
^ Err.Clear
^ Exit Function

End Function

The '^' are used above to simulate indentation.
Why does this code produce a runtime error 70 - permission denied -
message if I open the Error.csv file and cause an error to happen.
When the app tries to write to the opened Error.csv file and the OS
generates the 'permission denied' error should this code not just
result in the error being cleared and the function to be exited?

Bewildered, yet again.

Take care.

Mike
Reply all
Reply to author
Forward
0 new messages