Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping Errors In Forms with VBA Code

Status
Not open for further replies.

mas5

Programmer
Dec 15, 2002
98
0
0
US
I'm having a heck of a time with error trapping. Here's my situation and what I've done:

I have a numeric field. If the user enters a character in the field I try to trap it in the on exist event of the field as follows:

Private Sub SortPriority_Exit(Cancel As Integer)
On Error GoTo SortPriority_Err

'Code not related to error processing here

Exit_SortPriority:
Exit Sub

SortPriority_Err:
If IsNumeric(Me.SortPriority) = False Then
Cancel = True
Call InfoMessage(13)
Resume Exit_SortPriority
End If

End Sub

The above code does not trap the error. Instead the On Error event of my form traps the error. The code for this event is below:

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Call ErrorTrap(DataErr, "on error", "subschoolstaffcodes1", ErrorAction)
If ErrorAction = 0 Then
'what are the proper codes for the response?
End If
end sub


My ErrorTrap routine writes to an error table and looks up the error in another table that notes what should happen next. If erroraction = 0 = resume, 1=resume next, 2 = quit, etc. (I'm still working on this.)

I have several questions.

First, is there a way I can trap my error at the field level and not at the form level?

Second, sometimes I'm glad I can trap the error on the form level, but I have no idea what code to set Response to if I want to Resume, Resume Next, etc. Is it 0 or 1 or 2 or 3 or what? I'd like to be able to force error processing to execute the code in my on exist event.

Third, when I disable my error processing I get an error message from access: 2113 with a message "The value entered for this field isn't valid, etc." When I try to raise error 2113 the err.description I get is application error. How can I get the description that access gave me when I didn't trap the error?


Can anyone help? I've tried Access Help to no avail. I've read manuals. I've done keywork searches in these forums and have not found what I'm looking for. Thanks in advance.
 
Hallo,

You could use a Validation Rule (in the table field if it's a bound control), or maybe the Format or Input Mask properties,
depending on your exact requirements.

You could put your check in the BeforeUpdate event. That might work,

- Frink
 
That won't work. Perhaps I didn't state my question clearly enough. My issue is with error processing in general, not just this specific problem. I gave this problem as an example. My question is how can I trap ANY VB, ADO, ACCESS or any other error at the field level instead of at the form level. In this particular example, it is an ADO error that is generated before I'm even out of the field. It won't wait until the the beforeupdate event. The table is defined as an integer and that is what is generating the error message. I don't want my users to get a confusing error message from ADO - I want to give them a message myself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top