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 ofWklErrorCode) andErr.Source = "WklForms";Err.Descriptionholds 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&H8000FFFFwith “Internal error in the WKL Forms Object Library” and the type of the failure - report it to the supplier. Do not assume inCase Elsethat every number is awkeconstant. On Erroras usual. Handle them withOn Error GoToin the procedure that calls the member, and decide byErr.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
DataErrorof the tree and grid controls and ofWklTokenBox;Response = acDataErrContinue(0) suppresses the control's own message box. - Objects you hold - a
WklToastin aWithEventsvariable, aWklLinkManager, aRecordsetyou passed to a control - are released withSet ... = Nothingwhen the form closes.
Err.Number | Constant | Meaning |
|---|---|---|
&H80040301 | wkeNotConfigured | Data source or a required field is missing |
&H80040302 | wkeSourceError | Access reported an error while reading or saving (Access' text in Err.Description) |
&H80040303 | wkeDuplicateKey | Duplicate key |
&H80040304 | wkeCycle | Cycle in the parent-child relation |
&H80040305 | wkeKeyNotFound | No node or record with this key |
&H80040306 | wkeInvalidValue | Invalid value or invalid file |
&H80040307 | wkeNotAvailable | The 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.Numberof&H80040306is-2147220730in 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,WklTreeListorWklGanttthen 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 raiseswkeNotAvailableat 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.
Related topics
en/wiki/msaccess/comctl/how-to/handle-errors-of-wkl-controls.txt · Last modified: by Wolfgang van der Stille