Error info not available

L

Lars Brownies

I use the code beneath to trap for an error. I tested it with a required
field error, but the Err.number nor the Err.description is availabe when the
msgbox is fired. What am I doing wrong?

Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "An unexpected error has occurred in the form. " & _
"The error description is " & Err.Number & " " & Err.Description
Response = acDataErrContinue
End Sub

Thanks,
Lars
 
D

Dirk Goldgar

Lars Brownies said:
I use the code beneath to trap for an error. I tested it with a required
field error, but the Err.number nor the Err.description is availabe when
the msgbox is fired. What am I doing wrong?

Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "An unexpected error has occurred in the form. " & _
"The error description is " & Err.Number & " " & Err.Description
Response = acDataErrContinue
End Sub


In the Error event, the Err object has already been cleared. The DataErr
argument holds the error number. The specific description of the error
isn't available, but the general description for that error can be retrieved
by the expression

AccessError(DataErr)

So you could write:

"The error description is " & DataErr & " " & AccessError(DataErr)
 
L

Lars Brownies

Thanks Dirk. It works but not quite. The problem now is that
AccessError(DataErr) doesn't give the specific info about in what field the
error occured. The field name in the message is shown as |. If I want to
make a custom message about a required field error, I can't tell the user
which field it is.
Any ideas to also retrieve that info?
Lars
 
L

Lars Brownies

The Required field message occurs when a record is updated. At that moment
the particular field isn't necessarily the active one.

Lars
 
T

Tom Wickerath

Hi Lars,

Try using the Form_BeforeUpdate procedure. Here is an example:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ProcError

Dim blnValidate As Boolean
blnValidate = Validate

If blnValidate = False Then
Cancel = True
Err.Number = acDataErrContinue
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_BeforeUpdate event procedure..."
Resume ExitProc
End Sub


Private Function Validate() As Boolean
On Error GoTo ProcError

Validate = False 'Initialize return value

' Check for required field entries, if the form is dirty

If IsNull(cboProject) Then
MsgBox "Please Select a Project or click on the" & _
vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboProject.SetFocus
Exit Function
End If

If IsNull(txtRequestTitle) Then
MsgBox "Please Enter a Title or click on the" & _
vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestTitle.SetFocus
Exit Function
End If

If IsNull(txtRaisedDate) Then
MsgBox "Please Enter the Date Raised or click on the" & _
vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRaisedDate.SetFocus
Exit Function
Else
If Date < txtRaisedDate Then
MsgBox "You must not enter a future date", _
vbCritical, "Incorrect Date Entry..."
txtRaisedDate = Null
txtRaisedDate.SetFocus
Exit Function
End If
End If

Validate = True 'If we get this far, then all validation checks passed.
Me.txtLastUpdate = Date '<--If you have a LastUpdated field.
' Note: This textboxcan be a
hidden text box.

ExitProc:
Exit Function
ProcError:
Select Case Err.Number
Case 2110
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Validate procedure..."
End Select
Validate = False
Resume ExitProc
End Function



Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
L

Lars Brownies

Thanks for the tip, Tom.
Not what I hoped but I think this way you have easier control over the
errors, their messages, and subsequent actions.

Btw: in your case, are the particular 'required' fields really set to
'required' in the respective table or is this code what makes them required?

Lars
 
T

Tom Wickerath

Hi Lars,
Btw: in your case, are the particular 'required' fields really set to
'required' in the respective table or is this code what makes them required?

You can set the Required property to Yes, if you want to require an entry no
matter how someone gets to the table (ie. a query, another form, etc.).
However, if you do not set the Required property, then only the code would
make the field required. I tend to do both; the code in the BeforeUpdate
procedure allows one to provide a more user-friendly error message vs. the
JET database engine. Also, this code will cancle the update if the validation
fails. Therefore, one should never encounter the normal JET error message.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
L

Lars Brownies

Thanks a lot! Good idea.
Lars

Tom Wickerath said:
Hi Lars,


You can set the Required property to Yes, if you want to require an entry
no
matter how someone gets to the table (ie. a query, another form, etc.).
However, if you do not set the Required property, then only the code would
make the field required. I tend to do both; the code in the BeforeUpdate
procedure allows one to provide a more user-friendly error message vs. the
JET database engine. Also, this code will cancle the update if the
validation
fails. Therefore, one should never encounter the normal JET error message.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 

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