[AccessD] Trap for Error

Ken Ismert KIsmert at texassystems.com
Mon Nov 14 14:20:00 CST 2005


Tony, 

Here's some form-level error handling code I have written. I just drop
into my forms when I need form-level error handling. You can call if
from any procedure in the form, and hook it to the Form_Error event.

* It uses Screen.ActiveControl to find the offending control
* It assumes you have descriptive text in the control's .ValidationText
field

You can comment out parts you don't need, and customize the error
messages as you see fit.

Not fancy, but gets the job done.

-Ken

<code>
' Provides Customizable List of Errors for Form. Can be
' called from Procedure error handlers, or Form_Error
'
' Automatically handles User-defined errors in the range of
'    vbObjectError To (vbObjectError + 65535)
'
' Call from Procedure error handler:
'    ErrorDisplay "ProcName"
'
' Call from Form_Error:
'    Response = acDataErrContinue
'    ErrorDisplay Me.Name & ".Form_Error", DataErr
'
Private Sub ErrorDisplay(Optional ByVal sCalledBy As String = "", _
    Optional ByVal lAccessFormError As Long = 0)

    Dim rCtl As Access.Control
    Dim lErrNumber As Long
    Dim sErrDescription As String
    Dim sErrSource As String
    
    ' Record Error
    If Err.Number <> 0 Then
        lErrNumber = Err.Number
        sErrDescription = Err.Description
        sErrSource = Err.Source
    Else
        Debug.Assert lAccessFormError <> 0
        lErrNumber = lAccessFormError
        sErrDescription = AccessError(lAccessFormError)
        sErrSource = ""
    End If
    If sCalledBy > " " Then
        sErrSource = sCalledBy & vbCrLf & sErrSource
    End If

    ' Resets Err object
    On Error GoTo HandleErr

    Select Case lErrNumber
    Case pcErrVBInputMask
        Set rCtl = Screen.ActiveControl
        MsgBox rCtl.ValidationText, vbInformation, AppTitle
    Case pcErrJetFieldRequired
        ' Use "Not Is Null" validation on field
        Set rCtl = Screen.ActiveControl
        MsgBox rCtl.ValidationText, vbInformation, AppTitle
    Case pcErrJetDuplicateValues
        ' Duplicate Item number for BOM
        MsgBox "Duplicate Item Number" & vbCr & vbCr _
                & "Item " & txtItem & " is already in this BOM." & vbCr
& vbCr _
                & "Enter a unique new Item Number.", vbExclamation,
AppTitle
    Case pcErrJetFieldValidation, pcErrJetValidation, pcErrJetAppError
        ' Validation failed
        Set rCtl = Screen.ActiveControl
        MsgBox rCtl.ValidationText, vbInformation, AppTitle
    Case pcErrJetCantSaveObject
        MsgBox "Changes to this record are invalid and will not be
saved.", vbExclamation, AppTitle
    Case pcErrVBObjectVarNotSet
        ' Some earlier error forced reset of Interpreter
        MsgBox "Warning: " & vbCr & vbCr _
            & "This form has experienced a severe error," & vbCr _
            & "and will be closed." & vbCr & vbCr _
            & "Call IT with a description of the previous error.",
vbCritical, AppTitle
        DoCmd.Close acForm, Me.Name, acSavePrompt
    Case vbObjectError To (vbObjectError + 65535)
        ' Friendly Object Errors
        MsgBox sErrDescription, vbExclamation, AppTitle
    Case Else
        MsgBox "Error: " & lErrNumber & vbCrLf _
            & sErrDescription & vbCrLf _
            & "Source: " & sErrSource, vbCritical, AppTitle
    End Select
    
    Exit Sub

HandleErr:
    MsgBox "Error: " & Err.Number & vbCrLf _
        & Err.Description & vbCrLf _
        & "Source: ErrorDisplay" & vbCrLf & Err.Source, vbCritical,
AppTitle
    Exit Sub
End Sub
</code> 

-----Original Message-----
From: Tony Septav [mailto:iggy at nanaimo.ark.com] 
Sent: Sunday, November 13, 2005 2:46 PM
To: accessd at databaseadvisors.com
Subject: [AccessD] Trap for Error

Hey All
Thanks for your responses. 
That is what I was inquring about, the error message does not seem to be
fired by any  control event (but I will try them all again, I may have
missed something), but rather during  the process of pressing Ctrl V to
paste the text into the textbox. Anyway onwards and upwards, thanks
again.






More information about the AccessD mailing list