error handling (1 Viewer)

maxmangion

AWF VIP
Local time
Today, 01:56
Joined
Feb 26, 2003
Messages
2,805
when creating an error handling routine, if i would like to record the error message to a textfile, apart from just displaying a msgbox, is the following method correct or there is a better approach ?

Code:
Exit_Err_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error #" & Err.Number
Open "C:\Documents And Settings\user\Desktop\Error.log" For Output As #1
Print #1, Err.Description & Err.Number
Close #1
Resume Exit_Err_Handler
 

Rob.Mills

Registered User.
Local time
Yesterday, 20:56
Joined
Aug 29, 2002
Messages
871
The only way I know to write to a text file in access is to use the streamwriter.
 

Mile-O

Back once again...
Local time
Today, 01:56
Joined
Dec 10, 2002
Messages
11,316
Code:
    Open FilePath For [b]Append[/b] As #1
        Print #1, "YourMessage"
    Close #1
 

Mile-O

Back once again...
Local time
Today, 01:56
Joined
Dec 10, 2002
Messages
11,316
I'd also write the time of the error and who was using the database when it happened.
 

maxmangion

AWF VIP
Local time
Today, 01:56
Joined
Feb 26, 2003
Messages
2,805
thx for the tips ... however, if i put Append as you suggested, does the file creates itself automatically if it does not exits, or do i need to write some code to check if the file exists ?

Thanks
 

Mile-O

Back once again...
Local time
Today, 01:56
Joined
Dec 10, 2002
Messages
11,316
To be honest, I can't remember offhand. The APPEND will ensure that the text document is added to so all your errors are collected on the one text document rather than written over after every error.

Best just to try it out; if it can't find the text file then put a check in.
 

maxmangion

AWF VIP
Local time
Today, 01:56
Joined
Feb 26, 2003
Messages
2,805
thx for the tip. Actually, i've tried it out and there is no need to do a check in, because the Append will create the file automatically.

Thanks!
 

modest

Registered User.
Local time
Yesterday, 20:56
Joined
Jan 4, 2005
Messages
1,220
Here's one I put in a database before. It's nothing too intense... it just ensures that it goes in the same path as your db. I think I wrote it using the file scripting object because otherwise there were quotation marks around certain variables in the log.... I really don't remember though. Each code section is a certain module.


Code:
Option Compare Database

Public Sub LogActivity(strActivity As String, Optional ActivityInfo As String)

    Dim fileNum As Integer
    Dim filePath As String
    Dim strOut As String
    Dim LogFile As String
    Dim fs, File
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateFalse = 0

    
    filePath = GetFilePath(CurrentDb.Name)
    LogFile = filePath & "Activity.log"
    
    strOut = UCase$(Format(Now, "dd-MMM-yy hh:nn:ss")) & vbTab & fGetUserName & vbTab & strActivity & vbTab & ActivityInfo
    strOut = TrimNull(strOut)
    
[COLOR=Green]    'Get an available file number[/COLOR]
    fileNum = FreeFile
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    If Not fs.FileExists(LogFile) Then
        Set File = fs.CreateTextFile(LogFile, True)
        File.writeline ("     Time" & vbTab & vbTab & "User" & vbTab & "Activity" & vbTab & "Description")
        File.writeline ("-------------------------------------------------------------")
        File.Close
    End If
    
    Set File = fs.opentextfile(LogFile, ForAppending, TristateFalse)
    File.writeline strOut
    File.Close
[COLOR=Green]    '---The Following outputs as well---
    'Open LogFile For Append As fileNum
    'Write #fileNum, strOut
    'Close #fileNum[/COLOR]

End Sub

Public Function GetFilePath(strFileName As String) As String
    Dim intCounter As Integer
      
    For intCounter = Len(strFileName) To 1 Step -1
        If Mid$(strFileName, intCounter, 1) = "\" Then
            Exit For
        End If
    Next intCounter
    
    GetFilePath = Left$(strFileName, intCounter)
End Function


Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
        TrimNull = Left(strItem, intPos - 1)
    Else
        TrimNull = strItem
    End If
End Function


Code:
Option Compare Database

Private Declare Function apiGetUserName _
    Lib "advapi32.dll" _
    Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


[COLOR=Green]'_ This function is called from anywhere the programmer wishes _
 _ to get user's login name.  The function returns the login__ _
 _ name as a string. _________________________________________ _
'[/COLOR]
Function fGetUserName() As String

    Dim lngLength As Long
    Dim lngX As Long
    Dim strUser As String
    
    strUser = String$(254, 0)
    lngLength = 255
    lngX = apiGetUserName(strUser, lngLength)
    
    If (lngX > 0) Then
        fGetUserName = Left$(strUser, lngLength - 1)
    Else
        fGetUserName = vbNullString
    End If
End Function


Then I call it from anywhere in my program (normally a form load/unload event and error message handlers) using LogActivity "Error", "File Already Exists"

It's more useful to include the line number and the function name that the error occured in. Note that the function records the user and time. In my database I have a table that stores the user with the user computer name and on the database's first use I have the form request the user's real name.

This isn't supposed to be security intense as it would be easy to alter the file, just helpful in debugging and figuring out what caused the error.

**I know this is an old post, just hope it helps**
 

Users who are viewing this thread

Top Bottom