On error displayes default error code instead of the specifyed one

L

lord

Hi,

I have added error handling code to my combo box.

Private Sub searchName_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
On Error GoTo ERR_Handler

Set rs = Me.Recordset.Clone
rs.FindFirst "[sdutentId] = " & Str(Nz(Me![searchName], 0))
On Error GoTo 0
Exit Sub
ERR_Handler:
MsgBox "Please empty search box before continuing!"
End Sub



But it doesn't display the message in msgbox, it displayed default ms access
error code. which is "The text you have entered isn't an item in the list."

Any idea why would it do it?
 
A

Allen Browne

If the item is not in the list, the combo's NotInList event fires. The
combo's value doesn't get updated, and so its AfterUpdate event never runs.

Use its NotInList event. Here's some more examples of adding a value to the
lookup table:
http://allenbrowne.com/ser-27.html
 
L

lord

Thanks for reply,

the link that you have sent me is good for when the item is not in the list.

On my form, I need to add bunch of error messages for various cases,
starting from date format, to phone numbers format. So I wonder is there a
generic way to popup a message with an error, so that the user would know
what to do.

Thank you!
 
A

Allen Browne

Use the form's Error event to trap engine-level errors, e.g. wrong data
type, required field missing, unique index violation, referential integrity
violation.
 
L

lord

Would you be able to give an example on how to use those, and is there any
way to find out which error event was launched during an error.

Thank you!
 
A

Allen Browne

lord said:
Would you be able to give an example on how to use those, and is there
any way to find out which error event was launched during an error.

If it is the user (not code) that's doing the entry, you could examine
Form.ActiveControl to see which control has the focus.

I didn't follow the bit about the event that caused the error, as it's not
really event related but engine-related.

Here's an example of a generic function you could put into an standard
module, and then call it in the Error event of any form with:
Call FormError(Me, DataErr, Response)

Function FormError(frm As Form, DataErr As Integer, Response As Integer) As
Integer
On Error GoTo Err_FormError
'Purpose: Generic Form_Error handler.
'Return: Response.
'Usage: In a form's Error event procedure:
' Call FormError(Me, DataErr, Response)
Dim strMsg As String
Const conDupeIndex As Integer = 3022
Const conFileMissing As Integer = 3024
Const conRelatedRecordRequired As Integer = 3201
Const conWrongDataType As Integer = 2113

Select Case DataErr
Case conDupeIndex
strMsg = "The record cannot be saved, as it would create a
duplicate."
Response = acDataErrContinue

Case conRelatedRecordRequired
Response = acDataErrDisplay

Case conFileMissing
strMsg = "Data file is currently unavailable."
Response = acDataErrDisplay

Case conWrongDataType
strMsg = "The data type is not suitable."

Case Else
Response = acDataErrDisplay
End Select

If Len(strMsg) > 0 Then
MsgBox strMsg, vbExclamation, "CUSTOM TITLE"
End If
FormError = Response

Exit_FormError:
Exit Function

Err_FormError:
Call LogError(Err.Number, Err.Description, "FormError()", "DataErr: " &
DataErr)
Resume Exit_FormError
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top