Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Handle errors of WKL controls in VBA

How to: Handle errors of WKL controls in VBA

Catch the errors of WKL controls in your code the way you catch errors of Access, show the users a meaningful message instead of a VBA dialog, and release the objects you hold.

Requires WKL Forms Object Library 1.23 or later.

Solution overview

  • Fixed numbers. Every member of a WKL control or object that fails raises a run-time error with a fixed number (Err.Number, constants of WklErrorCode) and Err.Source = "WklForms"; Err.Description holds a text for the user in the language of the components, or Access's own text when Access refused.
  • Numbers outside WklErrorCode. An error Access raised reaches the caller with Access's own number and text; out of memory is &H8007000E; any other failure the components did not expect is &H8000FFFF with “Internal error in the WKL Forms Object Library” and the type of the failure - report it to the supplier. Do not assume in Case Else that every number is a wke constant.
  • On Error as usual. Handle them with On Error GoTo in the procedure that calls the member, and decide by Err.Number.
  • Errors without a caller - a load that fails while the control draws, a save of the user's edit that the data source refuses - arrive in the event DataError of the tree and grid controls and of WklTokenBox; Response = acDataErrContinue (0) suppresses the control's own message box.
  • Objects you hold - a WklToast in a WithEvents variable, a WklLinkManager, a Recordset you passed to a control - are released with Set ... = Nothing when the form closes.
Err.NumberConstantMeaning
&H80040301wkeNotConfiguredData source or a required field is missing
&H80040302wkeSourceErrorAccess reported an error while reading or saving (Access' text in Err.Description)
&H80040303wkeDuplicateKeyDuplicate key
&H80040304wkeCycleCycle in the parent-child relation
&H80040305wkeKeyNotFoundNo node or record with this key
&H80040306wkeInvalidValueInvalid value or invalid file
&H80040307wkeNotAvailableThe command is not available now (not form view, or not for the focused record)

Implementation

A command button that deletes the selected records of a grid, with a message for the cases the user can understand:

Private Sub cmdDelete_Click()
    On Error GoTo Failed
    Me!Grid1.Object.DeleteSelected
    Exit Sub
Failed:
    Select Case Err.Number
        Case wkeNotAvailable
            MsgBox "Bitte zuerst einen Datensatz auswählen.", vbInformation
        Case wkeSourceError
            MsgBox "Löschen nicht möglich: " & Err.Description, vbExclamation
        Case Else
            MsgBox "Fehler " & Hex(Err.Number) & " (" & Err.Source & "): " & Err.Description, vbCritical
    End Select
End Sub

A refusal of the data source while the user edits, shown in the form's own status line instead of a message box:

Private Sub Grid1_DataError(ByVal DataErr As Long, ByVal Description As String, Response As Integer)
    Me!lblStatus.Caption = Description
    Response = acDataErrContinue
End Sub

Releasing what the form holds:

Private WithEvents mToast As WklToast
 
Private Sub Form_Load()
    Set mToast = New WklToast
End Sub
 
Private Sub Form_Close()
    Set mToast = Nothing
End Sub

Remarks

  • Compare with the constants, not with negative decimal numbers: Err.Number of &H80040306 is -2147220730 in VBA, and the constants need the reference to WKL Forms Object Library (set by Access when a control is inserted).
  • An error inside an event procedure that you do not handle shows the VBA run-time error dialog, as for any Access event; handle errors in event procedures as well.
  • Code that calls a member during a running event of the same control: a record command of WklGrid, WklTreeList or WklGantt then runs after the event procedure, so a failure while it runs does not reach the calling code; only its availability is checked at once and raises wkeNotAvailable at the call.
  • Error texts follow the language of the components (English, German, Slovenian, Croatian or Italian; English when none applies), like the rest of their user interface. Decide by Err.Number, never by the text: the number stays the same in every language. Choosing the language: How to: Choose the language of the components.
en/wiki/msaccess/comctl/how-to/handle-errors-of-wkl-controls.txt · Last modified: by Wolfgang van der Stille