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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Trapping

Status
Not open for further replies.

striker73

MIS
Jun 7, 2001
376
US
I am trying to do some of my own error trapping, but I'm not exactly sure how to do it correctly. I have a form where a user can enter information and add it to a table. I am just trying to make the form more robust, so when a user types something, but hits the close button, it will ask the user if he wants to close the form without saving or not. So far this part works fine.

I'm running into a problem where the user says "no, I hit the close button on accident, I want to return to the form just as it is." I can't get access to stop closing the form. Any ideas? Here is some of my code:

Private Sub Form_Error(DataError As Integer, Response as Integer)
Dim UserResponse as Integer

'Error where a required field is not filled out
If (DataErr = 3314) Then
If (FormClean = False) Then 'FORM IS DIRTY
UserResponse = MsgBox ("Close form?", vbYesNoCancel)
If (UserResponse=Yes) Then
Me.Undo
Response = acDataErrContinue
keepopen=false

Else
'Stop closing form and return to normal state
Response = acDataErrContinue
keepopen=true
End If

Else 'FORM IS CLEAN
Response = acDataErrContinue
End If

ElseIf (DataErr = 2169)
'This error says Access encountered an error
'while trying to save a record. Error message asks
'if I still want to close the database object?
'I am trying to answer it with yes or no.

Const ResponseNo as Integer = 7
Const ResponseYes as Integer = 6

If (keepopen = false) Then
msgbox ("Keep the form open!!")
Response=ResponseNo
Else
msgbox ("Close the form!!!")
Response=ResponseYes
End
End If


The error message for 2169 gives me a yes or no option. I am trying to figure out a way I can pre-select the answer for the user. On MSDN, it says you can have Response be acDataErrContinue, (where it surpresses the error message and continues), acDataErrDisplay (where it shows the error message) or a constant. I'm trying to use constants 6 and 7 for yes and no, respectively.

Am I anywhere near the right track or am I just treading water? Any help would be appreciated. Thanks!!!!!!!




 
You should be able to use the 'docmd.cancelevent' method to stop the form from closing. The hardest questions always have the easiest answers.
 
Here is a little of my revised code:

If (UserResponse=Yes) Then
Me.Undo
Response = acDataErrContinue
Else
'Cancel the event
DoCmd.CancelEvent
MsgBox("Event didn't cancel.")
End If

This code is inside the Private Sub Form_Error() event. Theoretically, it should stop the event before it gets to the message box, but it doesn't. Any ideas? Thanks!
 
If you paste the following code into the unload event of your form it should give you the ?funcinality? that you desire.
Code:
If MsgBox("Leave the form open", vbYesNo, "Leave Open?") = vbYes Then
   DoCmd.CancelEvent
End If
The two problems that I forsee with this is that.
1. It does not use the original code that you had, you may be trying to do something more difficult than I see by reading your code.
2. You cannot get the dirty state unless the form holds a record from a table. Although this may not be a problem. It looks like you already figured out how to get a dirty variable. Unless that comes with the Form_Error? The hardest questions always have the easiest answers.
 
A couple of observations.

First, use the select case in you error code in conjunction with an error trap. Put it in whatever event will cause the error (most likely commandX On_click)

Example:

Private Sub commandX On_Click()
On Error go to Err_Hand
'code that does stuff here
'at the bottom of the page now
Err_Hand:
Select case err.number
Case 3314
'Do something
Case 2169
'Do something else
End Select
End Sub

Also, the variable Response is passed INTO the form on_Error event. Re declaring response really isn't doing anything because it is implicitly declared in the sub.

Tyrone Lumley
augerinn@gte.net
 
I tried it, but it didn't work. Earlier, I had my code in the Form_Error() event. When I put this code in the form unload event, it never ran.

When I had the code in the Form_Error() event, and when I put DoCmd.CancelEvent... I would run the debugger and it would go through that line, but it wouldn't cancel the event. It would continue with the event. I looked up "DoCmd.CancelEvent" on MSDN and it says:

"The CancelEvent method has an effect only when it's run as the result of an event. This method cancels the event."

What does it mean that it only works if it is the result of an event? The Form_Error event doesn't return anything.
 
What's really triggering the event is when the user hits the X close button in the top right corner, so I can't just put error code in that private sub. :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top