Form_Error and Err object

G

Guest

For errors occurring in the context of controls, I can use the Err object to
capture the error number (Err.Number) and description (Err.Description).
However, I am not sure how to do this within the context of Form_Error. I
seem to be able to capture the error number, but not its description.

Here is my Form_Error Sub (goes on the Error event of all forms):

Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo ErrorHandler
DataErrCode = DataErr
ErrorHandlerForm
Response = ResponseCode
End Sub

Here, now, is a snippet of the Public Sub in a code module that handles this:

Public Sub ErrorHandlerForm()
Select Case DataErrCode
Case Is = 2113 'data type mismatch
If MsgBox("The error code is " & DataErrCode & ". Do you want to
see the system error message?", vbYesNo, "Error Code") = vbNo Then
ResponseCode = 0
Else
ResponseCode = 1
End If
Case Is = 2116 'validation rule error; handled by code
ResponseCode = 0
End Select
End Sub

It seems that the form error description does not appear until I send back a
response to the Form_Error event, and then I do not know how to capture the
description string. All I can do is, by using my ResponseCode public
variable, either show or hide the system error message. Howeer, I am now
implementing this in a non-interactive (automated) application that needs to
log the error message and send me an e-mail notification instead of
displaying the message on-screen.
 
A

Allen Browne

Form_Error does not expose the message.

You can get the generic version of the error description by opening the
Immediate Window (Ctrl+G), and entering:
? AccessError(2113)
 
G

Guest

Thanks, Allen. I just did some trial-and-error and found that I can pass the
error number to a function using a public variable frmErrNum and then capture
the description as AccessError(frmErrNum) so that I can log it along with the
code.
 
A

Allen Browne

Yep. That's great, except we still don't get the specifics.

For example, for 3314, the template message says:
The field '|' cannot contain a Null value because the Required
property for this field is set to True.
and we do not know which field the message actually referenced.

I do not have a better suggestion though. (It might be Form.ActiveControl,
but it might not be too.)
 
G

Guest

What's strange is that when I return a response to Form_Error, the correct
message is displayed, but I cannot seem to capture it prior to that point.
Here is my forms' error event (the same for every form; ResponseCode and
DataErrCode are public Integer variables):

Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo ErrorHandler
DataErrCode = DataErr
ErrorHandlerForm
Response = ResponseCode
End Sub

Here is my public ErrorHandlerForm procedure called by Form_Error:

Public Sub ErrorHandlerForm()
DoCmd.SetWarnings True
Select Case DataErrCode
Case Is = 3314 'required field is null
MsgBox AccessError(3314)
If MsgBox("Required information has not been entered. Do you want to
see more specific error information?", vbCritical + vbYesNo, "Missing
Information") = vbYes Then
If MsgBox("The error code is " & DataErrCode & ". Do you want to
see the system error message?", vbYesNo, "Error Code") = vbNo Then
ResponseCode = 0
Else
ResponseCode = 1
End If
End If
Exit Sub

End Select
'Display error information.
Set frmCrnt = Screen.ActiveForm
MsgBox "Please report the following information to the programmer along with
the next error message. Form: " & frmCrnt.Name & ". Error number " &
DataErrCode, vbCritical, "Form Error"
ResponseCode = 1
End Sub

As you can see, it first generates a user-friendly error message, then
offers to show more specific info. If the user clicks "Yes", it shows the
error # and offers to show the system error message. If the user again,
clicks "Yes", it shows the full error message, including the field name for
the 3314 error, presumably as a result of the Response being 1 when returning
from the public procedure. When I inserted this: MsgBox
AccessError(DataErrCode) inside the ErrorHandlerForm procedure, it generates
the generic message you indicated.

I'm still a little confused as to why it can display the complete message at
the end of all of this but will not allow me to capture it in the process.

Form.ActiveControl works for errors generated in the context of controls (I
have a separate public procedure that deals with those and the Err object
inherently associated with them, since it has a Description property that I
can display).
 
A

Allen Browne

Brian, I haven't tried this, but if you are getting a full error message at
some point, you might try assigning it to a string variable, and passing
that string to your error logger.
 
G

Guest

At this point, I cannot see that there is any way to capture the error
description string (unlike Err.Description); it just pops up in a popup box.
I do know this, though: the complete message does not appear until I set
Response to 1. If I insert MsgBox AccessError(DataErrCode) into the
ErrorHandlerForm procedure, it comes back blank. I guess I can just live with
logging the generic message and capturing its context (the form). These are
in the minority, I believe, since many errors occur in the context of a
control, in which case I can refer to the Error using Err.Number &
Err.Description. Maybe there is just something I don't understand about what
is happening here.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo ErrorHandler
DataErrCode = DataErr 'assign error number to public variable to transport
it to the public procedure
ErrorHandlerForm 'call public procedure
Response = ResponseCode 'generates complete error message if ResponseCode
=1; no message if ResponseCode=0
End Sub
 

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