VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Class to handle errors. Use this to easily handle errors in your VB application by creating a class

by David Combs (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 31st May 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Class to handle errors. Use this to easily handle errors in your VB application by creating a class module and copying the code directly into

API Declarations


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

' API Function to get the computer name
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, _
ByRef nSize As Long) As Long

Private m_strFilePath As String ' path to save the log
Private m_blnLoggingOn As Boolean ' turn logging on or off
Private m_strUserName As String ' store the user name for logging purposes
Private m_strComputerName As String ' store the name of the computer the error occurred on

Rate Class to handle errors. Use this to easily handle errors in your VB application by creating a class



    If Len(v_strPath) > 0 Then m_strFilePath = v_strPath
End Property

Public Property Let AssignUserName(ByVal v_strUser As String)
    If Len(v_strUser) > 0 Then m_strUserName = v_strUser
End Property

Private Sub Class_Initialize()
    ' PURPOSE: sets the defaults of the log file path and user name.  The computer name is
    ' not an option.
    ' INPUTS: None
    ' ASSUMES: log file path and user name are defaulted
    ' RETURNS: None

    Const c_strBackSlash As String = "\"

    ' Check for back slash in the Application Path
    If Right$(App.Path, Len(c_strBackSlash)) = c_strBackSlash Then
        m_strFilePath = App.Path
    Else
        m_strFilePath = App.Path & c_strBackSlash
    End If

    m_strFilePath = m_strFilePath & App.EXEName & Format(Date, "yyyymmdd") & ".log"
    m_strUserName = m_strGetUserName()
    m_strComputerName = m_strGetComputerName()
    m_blnLoggingOn = False  ' turn logging off when class is initialized

End Sub

Private Sub m_LogError(ByVal v_objError As ErrObject, _
                       ByVal v_strReportedBy As String)
    ' PURPOSE: Writes the error to a log the user specifies or the application folder
    ' INPUTS: v_objError - the current error that has occurred
    '         v_strReportedBy - what procedure reported the error
    ' ASSUMES: None
    ' RETURNS: None

    Dim intLogFile As Integer
    Dim datDateTime As Date
    
    intLogFile = FreeFile()
    Open m_strFilePath For Append Access Write As intLogFile
    
    datDateTime = CDate(Format(Now(), "mm/dd/yyyy hh:nn:ss"))
    
    Print #intLogFile, "DATE       : "; datDateTime
    Print #intLogFile, "APPLICATION: "; App.EXEName
    Print #intLogFile, "NUMBER     : "; v_objError.Number
    Print #intLogFile, "REPORTED BY: "; v_strReportedBy
    Print #intLogFile, "SOURCE     : "; v_objError.Source
    Print #intLogFile, "DESCRIPTION: "; v_objError.Description
    Print #intLogFile, "USER       : "; m_strUserName
    Print #intLogFile, "COMPUTER   : "; m_strComputerName
    Print #intLogFile,
    
    Close #intLogFile
    
End Sub

Private Function m_strGetComputerName() As String
    ' PURPOSE: will get the computer name
    ' INPUTS: None
    ' ASSUMES: None
    ' RETURNS: the computer name or c_strNoComputerName

    Dim strString As String
    Dim lngSize As Long
    Dim lngReturnValue As Long
    Const c_lngBufferSize As Long = 255
    Const c_strNoComputerName As String = "NoComputerName"
    
    strString = String$(c_lngBufferSize, vbNullChar)
    lngSize = c_lngBufferSize
    lngReturnValue = GetComputerName(strString, lngSize)
    
    If lngReturnValue <> 0 Then
        m_strGetComputerName = Left$(strString, lngSize)
    Else
        m_strGetComputerName = c_strNoComputerName
    End If
    
End Function

Private Function m_strGetUserName() As String
    ' PURPOSE: will get the Windows logon name
    ' INPUTS: None
    ' ASSUMES: None
    ' RETURNS: the Windows logon name or c_strNoUserName
    
    Dim lngReturnValue As Long
    Dim strString As String
    Dim lngSize As Long
    Const c_lngBufferSize As Long = 255
    Const c_strNoUserName As String = "000000"
    
    strString = String$(c_lngBufferSize, vbNullChar)
    lngSize = c_lngBufferSize
    lngReturnValue = GetUserName(strString, lngSize)
    
    If lngReturnValue <> 0 Then
        m_strGetUserName = Left$(strString, lngSize - 1)
    Else
        m_strGetUserName = c_strNoUserName
    End If
    
End Function

Private Function m_strMakeCallPath(ByVal v_strProcSig As String, _
                                   ByVal v_strErrorSource As String) As String
    ' PURPOSE: To create the path of the error as it is bubbled up the call stack
    ' INPUTS: v_strProcSig - the name of the procedure where the error is raised
    '         v_strErrorSource - the name of the error source
    ' ASSUMES: None
    ' RETURNS: The call path of the error

    Const c_strCallPathSeparator As String = " | "
    
    m_strMakeCallPath = v_strProcSig & c_strCallPathSeparator & v_strErrorSource
    
End Function

Public Sub RaiseError(ByVal v_strProcSig As String)
    ' PURPOSE: allows the user to raise an error when there is no error handling routine
    ' in the procedure from which this procedure was called
    ' INPUTS: v_strProcSig - name of the procedure where the error occurred
    ' ASSUMES: None
    ' RETURNS: None

    Err.Raise Err.Number, m_strMakeCallPath(v_strProcSig, Err.Source), Err.Description
    
End Sub

Public Sub ShowError(ByVal v_objError As ErrObject, _
                     ByVal v_strReportedBy As String)
    ' PURPOSE: Displays the error to the user and if logging is turned on, it logs the
    ' error to a file
    ' INPUTS: v_objError - the current error that has occurred
    '         v_strReportedBy - what procedure reported the error
    ' ASSUMES: None
    ' RETURNS: None

    Call MsgBox("Error: " & v_objError.Number & vbCrLf & _
        "Reported By: " & v_strReportedBy & vbCrLf & _
        "Source: " & v_objError.Source & vbCrLf & _
        "Description: " & v_objError.Description, vbInformation, "Error")
        
    If m_blnLoggingOn Then Call m_LogError(v_objError, v_strReportedBy)
    
End Sub

Public Property Let TurnLoggingOn(ByVal v_blnLoggingOn As Boolean)
    m_blnLoggingOn = v_blnLoggingOn
End Property




Download this snippet    Add to My Saved Code

Class to handle errors. Use this to easily handle errors in your VB application by creating a class Comments

No comments have been posted about Class to handle errors. Use this to easily handle errors in your VB application by creating a class. Why not be the first to post a comment about Class to handle errors. Use this to easily handle errors in your VB application by creating a class.

Post your comment

Subject:
Message:
0/1000 characters