newman     VBWeb     VBSource     Portfolio    

ErrorMsg

AutoTab

TOP TEN

FileExists
Pops up a nice error message and writes an error log line.
Syntax
ErrorMsg ProcedureName, MoreHelp
Call ErrorMsg(ProcedureName, MoreHelp)
Part
Description
ProcedureName Required text that is the name of the procedure that trapped the error. This text is not shown to the user but goes into the log.
MoreHelp Optional text that goes into the pop-up box to provide more information on what the problem might be.


Remarks
Putting an error trap in every event makes your VB application virtually bullet proof. In addition to popping up a message box, it also writes a line to an error log. The default error log name looks like C:\Windows\appname.LOG where C:\Windows is the Windows directory and appname is the name of the executable. Set gErrorLogName to another filename and path to override the default error log. Set gIsNoErrorLog to True to skip the error log.

NOTICE: ErrorLog calls the WinDir() function.

Example
	Sub cmdOk_Click ()
	    Const PROC = "cmdOk_Click"
	    On Error Goto Trap
	    Call OkSubProc
	    Exit Sub
	Trap:
	    Call ErrorMsg(PROC, "")
	    Exit Sub
	End Sub
	
Source
Global gIsNoErrorLog As Integer
Global gErrorLogName As String

Sub ErrorLog(ByVal Procedure As String, _
    ByVal ErrNum As Integer, _
    ByVal ErrMsg As String, _
    ByVal Help As String)
		
    Dim hFile As Integer
    Dim Msg As String

    On Error GoTo ErrorLogTrap
    If gErrorLogName = "" Then
        gErrorLogName = WinDir() & "\" & App.EXEName & ".LOG"
    End If
    Msg = App.EXEName & Format$(Now, " mm/dd/yy-hh:nnam/pm ")
    Msg = Msg & Format$(ErrNum, "00000")
    If ErrMsg > "" Then Msg = Msg & " - " & ErrMsg
    If Help > "" Then Msg = Msg & " - Help: " & Help
    If Procedure > "" Then
        Msg = Msg & " - Procedure: " & Procedure
    End If
    hFile = FreeFile
    Open gErrorLogName For Append As hFile
    Print #hFile, Msg

ErrorLogTrap:
    Close hFile
    Exit Sub
End Sub

Sub ErrorMsg(ByVal Procedure As String, ByVal Help As String)
    Const UDE = "User-defined error"
    Dim ErrNum As Long
    Dim ErrMsg As String
    Dim Msg As String

    ErrNum = Err
    ErrMsg = Error$
    On Error GoTo ErrorMsgTrap
    If ErrMsg > "" And ErrMsg <> UDE Then
        Msg = ErrMsg & vbCrLf & vbCrLf
    End If
    If Help > "" Then Msg = Msg & Help & vbCrLf & vbCrLf
    Msg = Msg & " Your request might not have"
    Msg = Msg & " been completed as expected."
    If Not gIsNoErrorLog Then
        Call ErrorLog(Procedure, ErrNum, ErrMsg, Help)
        Msg = Msg & " See " & gErrorLogName & "."
    End If
    Beep
    Screen.MousePointer = vbDefault
    MsgBox Msg
    Exit Sub

ErrorMsgTrap:
    Exit Sub
End Sub
	
top

AutoTab

TOP TEN

FileExists


Feedback Comments
Return Address (if you want a reply)


Copyright 1997 NEWMAN Services Corp. All rights reserved.
973-228-5753 pres@newmanservices.com
DECONSTRUCT THIS PAGE