Results 1 to 7 of 7

Thread: Error handling/trapping.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    Atlanta
    Posts
    104
    I've set up some error handling, and I have a few questions
    about the process I've used.

    One of my questions is how do I kill the application after
    logging the error?

    The second question is more an issue of "the right way to
    do things". Can the process I've used be improved upon?

    And finally, is there ANY documentation out there for error
    handling? I've looked around quite a bit and haven't found
    anything greatly useful or enlightening.

    Here's a sample of how I've set it up.
    Code:
    Public Function IshiFishi()
    On Error Goto Error
    
        Misc code here...
    
    Exit Function
    
    Error:
    WriteError Err.Number, Err.Description, "Additional info."
    End Function
    
    Sub WriteError(ErrNum As Integer, Desc As String, Message As String)
        If Message = "" Then Message = "An unknown error has occured." & vbCrLf Else Message = Message & vbCrLf
        Dim F As Integer
        F = FreeFile
        MsgBox ErrNum & vbCrLf & Message & Desc & vbCrLf & "Please contact your technical support group.", vbOKOnly, "ERROR!"
        UsageLog "User received error: " & Message
        Open "error.log" For Append As #F
          Print #F, ErrNum & " - " & Date$ & " - " & Time$ & " - "
          Print #F, Message
          Print #F, Desc
          Print #F, String$(60, "-")
        Close #F
    End Sub

  2. #2
    Guest
    1) Put the word end where you want to terminate the program. UNLESS it isn't your program you want to terminate.

    2) I can't see how your code could be improved.

    3) I'm sure if you look hard enough, or post another thread about errors, you'll get it.

  3. #3
    Guest
    Q1:

    Unload Me

    Q2:

    It probably could be improved, but that is a good way. It is kind of hard to capture all errors.

    Q3:

    Hit F1 when the error comes up (the real error, not your generated one) and VB will tell you what it is.
    If that don't help, search the MSDN site for the error.

  4. #4
    Guest
    Originally posted by Dreamlax
    1) Put the word end where you want to terminate the program. UNLESS it isn't your program you want to terminate.
    Never use the END statement by itself. From the VB Help File (MSDN Library):

    End Terminates execution. Never required by itself but may be placed anywhere in a procedure to close files opened with the Open statement and to clear variables.

  5. #5
    Guest
    I never knew about that. I thought End just ENDed your program. Oh well, I'll keep that in mind next time I write something.

  6. #6
    Lively Member
    Join Date
    Jan 1999
    Location
    India
    Posts
    85

    Error Handling

    This is how we implemented <font color="#00007F">Error</font> Handling. We also had EventLogger and TimeLogger Functions.

    [code]
    <font color="#00007F">Public</font> <font color="#00007F">Sub</font> ErrorLogger(<font color="#00007F">ByVal</font> Application <font color="#00007F">As</font> <font color="#00007F">String</font>, _
    <font color="#00007F">ByVal</font> ModuleName <font color="#00007F">As</font> <font color="#00007F">String</font>, _
    <font color="#00007F">ByVal</font> FunctionName <font color="#00007F">As</font> <font color="#00007F">String</font>, _
    Optional <font color="#00007F">ByVal</font> UserID <font color="#00007F">As</font> <font color="#00007F">String</font> = "Unknown", _
    Optional <font color="#00007F">ByVal</font> Parameters <font color="#00007F">As</font> <font color="#00007F">String</font> = "None", _
    Optional <font color="#00007F">ByVal</font> ErrorMessage <font color="#00007F">As</font> <font color="#00007F">String</font> = "", _
    Optional <font color="#00007F">ByVal</font> LogFilePath <font color="#00007F">As</font> <font color="#00007F">String</font> = "")

    WriteToFile "Error", Application & vbTab & _
    ModuleName & vbTab & _
    FunctionName & vbTab & _
    UserID & vbTab & _
    <font color="#00007F">Now</font> & vbTab & _
    Parameters & vbTab & _
    ErrorMessage, LogFilePath
    <font color="#00007F">End</font> <font color="#00007F">Sub</font>


    <font color="#00007F">Private</font> <font color="#00007F">Sub</font> WriteToFile(<font color="#00007F">ByVal</font> sFileType <font color="#00007F">As</font> <font color="#00007F">String</font>, _
    <font color="#00007F">ByVal</font> sInformation <font color="#00007F">As</font> <font color="#00007F">String</font>, _
    Optional <font color="#00007F">ByVal</font> sLogFilePath <font color="#00007F">As</font> <font color="#00007F">String</font> = "")
    <font color="#00007F">On</font> <font color="#00007F">Error</font> <font color="#00007F">Resume</font> <font color="#00007F">Next</font>
    <font color="#00007F">Dim</font> fsoFobj <font color="#00007F">As</font> <font color="#00007F">New</font> FileSystemObject
    <font color="#00007F">Dim</font> fsoFile
    <font color="#00007F">Dim</font> sFileName <font color="#00007F">As</font> <font color="#00007F">String</font>
    <font color="#00007F">Dim</font> sHeader <font color="#00007F">As</font> <font color="#00007F">String</font>
    <font color="#00007F">Dim</font> bFileExist <font color="#00007F">As</font> <font color="#00007F">Boolean</font>
    <font color="#00007F">If</font> <font color="#00007F">Len</font>(sLogFilePath) <= 0 <font color="#00007F">Then</font> sLogFilePath = App.Path & "\LogFiles"
    <font color="#00007F">If</font> <font color="#00007F">Not</font> fsoFobj.FolderExists(sLogFilePath) <font color="#00007F">Then</font> fsoFobj.CreateFolder sLogFilePath
    sFileName = sLogFilePath & "\" & sFileType & "Log" & <font color="#00007F">Format</font>(<font color="#00007F">Now</font>, "yyyy-mm-dd") & ".xls"
    bFileExist = fsoFobj.FileExists(sFileName)
    <font color="#00007F">Set</font> fsoFile = fsoFobj.OpenTextFile(sFileName, ForAppending, True, TristateFalse)
    <font color="#00007F">If</font> <font color="#00007F">Not</font> bFileExist <font color="#00007F">Then</font>
    <font color="#00007F">Select</font> <font color="#00007F">Case</font> sFileType
    <font color="#00007F">Case</font> "Error": sHeader = "Application" & vbTab & "Module" & vbTab & "Function" & vbTab & "User" & vbTab & "Time" & vbTab & "Parameters" & vbTab & "ErrorMessage"
    <font color="#00007F">Case</font> "Event": sHeader = "Application" & vbTab & "Module" & vbTab & "Function" & vbTab & "User" & vbTab & "Time" & vbTab & "Parameters"
    <font color="#00007F">Case</font> "Time": sHeader = "Application" & vbTab & "ConnString" & vbTab & "SQL" & vbTab & "User" & vbTab & "Time" & vbTab & "ElapsedTime"
    <font color="#00007F">End</font> <font color="#00007F">Select</font>
    fsoFile.Write sHeader & vbCrLf
    <font color="#00007F">End</font> <font color="#00007F">If</font>
    fsoFile.Write sInformation & vbCrLf
    fsoFile.<font color="#00007F">Close</font>
    <font color="#00007F">Set</font> fsoFobj = Nothing
    <font color="#00007F">Set</font> fsoFile = Nothing
    <font color="#00007F">End</font> <font color="#00007F">Sub</font>

    [code]
    Thanks
    Ramdas

  7. #7
    Lively Member
    Join Date
    Jan 1999
    Location
    India
    Posts
    85

    Error Handling

    This is how we implemented Error Handling. We also had EventLogger and TimeLogger Functions.

    [code]
    Public Sub ErrorLogger(ByVal Application As String, _
    ByVal ModuleName As String, _
    ByVal FunctionName As String, _
    Optional ByVal UserID As String = "Unknown", _
    Optional ByVal Parameters As String = "None", _
    Optional ByVal ErrorMessage As String = "", _
    Optional ByVal LogFilePath As String = "")

    WriteToFile "Error", Application & vbTab & _
    ModuleName & vbTab & _
    FunctionName & vbTab & _
    UserID & vbTab & _
    Now & vbTab & _
    Parameters & vbTab & _
    ErrorMessage, LogFilePath
    End Sub


    Private Sub WriteToFile(ByVal sFileType As String, _
    ByVal sInformation As String, _
    Optional ByVal sLogFilePath As String = "")
    On Error Resume Next
    Dim fsoFobj As New FileSystemObject
    Dim fsoFile
    Dim sFileName As String
    Dim sHeader As String
    Dim bFileExist As Boolean
    If Len(sLogFilePath) <= 0 Then sLogFilePath = App.Path & "\LogFiles"
    If Not fsoFobj.FolderExists(sLogFilePath) Then fsoFobj.CreateFolder sLogFilePath
    sFileName = sLogFilePath & "\" & sFileType & "Log" & Format(Now, "yyyy-mm-dd") & ".xls"
    bFileExist = fsoFobj.FileExists(sFileName)
    Set fsoFile = fsoFobj.OpenTextFile(sFileName, ForAppending, True, TristateFalse)
    If Not bFileExist Then
    Select Case sFileType
    Case "Error": sHeader = "Application" & vbTab & "Module" & vbTab & "Function" & vbTab & "User" & vbTab & "Time" & vbTab & "Parameters" & vbTab & "ErrorMessage"
    Case "Event": sHeader = "Application" & vbTab & "Module" & vbTab & "Function" & vbTab & "User" & vbTab & "Time" & vbTab & "Parameters"
    Case "Time": sHeader = "Application" & vbTab & "ConnString" & vbTab & "SQL" & vbTab & "User" & vbTab & "Time" & vbTab & "ElapsedTime"
    End Select
    fsoFile.Write sHeader & vbCrLf
    End If
    fsoFile.Write sInformation & vbCrLf
    fsoFile.Close
    Set fsoFobj = Nothing
    Set fsoFile = Nothing
    End Sub

    [code]
    Thanks
    Ramdas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width