Can't find a way to trap error!

I did not suggest kicking "On Error GoTo btnNext_Click_Err" !!! Thatis your error handler !

No, you didn't. I should have said I tried OnErrorNext first, and it didn't help.

It just seems to die on DoCmd.GoToRecord , "", acNext no matter what I do.

I think that all I've accomplished was to move the problem over to VBA, but not solve anything.

If my feeling is correct about the problem having to do with the macro code, that is still happening because I'm now calling the macro function "GoToRecord" from code.

It almost seems like VBA can't trap the macro error...
 
Sorry, but the really basic stuff you have to sort out on your own.
 
Hi All,

I've decided to start from scratch, and build a new database that demonstrates the problem I'm having.

The test database, attached, has one table with two columns, "FirstName" and "LastName" along with one Form, named "Test1."

I created five record navigation buttons on the Form, by using the "Command Button Wizard", which creates a Macro for record navigation such as "Go To First Record" and "Go To Previous Record."

After I created the five record navigation buttons, I used the "Convert Form's Macros To Visual Basic" feature in "Database Tools" to convert all of the Macros to code.

I then created another button, "Previous Record (Macro)" and also used the "Command Button Wizard" to create a "Go To Previous Record" Macro for the button. I did not convert this Macro to code.

I've set the database to automatically open the "Test1" form.

When you are on the first record, click on the "previous" button with the left-arrow graphic. You will get a "Runtime Error 2105" error. This error pops up even though there is OnError code in the click event for this button.

Also when you are on the first record, click on the "Previous Record (Macro)." This results in a MsgBox with "You can't go to the specified record." This MsgBox is generated by the Macro code behind the "Previous Record (Macro)" button. This behavior is expected, since the Macro is responsible for generating the MsgBox

My original issue was that I was trying to trap the MsgBox in the Macro. My "solution" was to convert the Macro to code.

However, that created a new issue where a runtime error message is generated, even though there is OnError code in the click event.

Does anyone have an idea why there is a runtime error being generated, and how to stop it?

Thanks!
 

Attachments

I see the onset of panic. See a movie, read a book, go for a walk, have a bonk. Sleep. This is really really basic stuff, and you got all the pointers required to deal with it. Tomorrow the solution will come to you.
 
Good advice. Thanks.

But I don't see how this is basic.

Here's an event with error trapping code that's not trapping errors...

And I've already bonked my head on the desk, and it hasn't helped....

;)
 
The kind of bonk I had in mind was *stricken by the decency-police*

But the rest remains. Take a break. It is simple.
 
I agree with spikepl... time to take a break.

Also, not everyone has Acc2007 or 2010, so can not open an accdb.
If you include an mdb version of you database, you may get more responses.
 
If your goal is to NOT get the standard msgbox then clear the macro errorobject.

In your macro after this statement.

If [MacroError] <> 0 Then

choose action:

ClearMacroError

This will reset the errorobject to 0 and nothing will happen when when you are on the first record and try to move to the previous.

JR
 
Your converted macro buttons don't error because you have On Error Resume Next so I don't understand why you say it errors. I've just downloaded your test db and it works fine.

But remember to Err.Clear if you're going to use Resume Next. If using a macro JR covers that in his post.
 
One other thing is that using

If MacroError <> 0

could be dangerous because ALL errors will be reset, it would be wise to only trapp a specific and anticipated errors. In this case you would trap errornumber 2105

so i would use

If [MacroError] = 2105 Then
ClearMacroError
Else
Msgbox MacroError.Description, vbOKOnly, ""
End If

Jr
 
@Melissa_May1:
Using "On Error Resume Next" mean all errors from this point will not be traped.
Not only they won't be traped they will also be ignored as the line caused the error will be ignored and code will continue to the next line.

using error traper as used in your code, will cause the code (In case of error) to jump to the blue line, show the message box in green and jump from the brown line to the red one, and exit the sub.
The error traper will be activated by the orange line.
Code:
Private Sub btnNext_Click()
[COLOR=orange]On Error GoTo btnNext_Click_Err[/COLOR]
 
    [COLOR=purple]On Error Resume Next[/COLOR]
    DoCmd.GoToRecord , "", acNext
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If
 
 
[COLOR=red]btnNext_Click_Exit[/COLOR]:
    Exit Sub
 
[COLOR=blue]btnNext_Click_Err[/COLOR]:
    [COLOR=seagreen]MsgBox Error$[/COLOR]
    [COLOR=cyan][COLOR=darkred]Resume btnNext_Click_Exit[/COLOR]
 
[/COLOR]End Sub

This is very good for normal working, but when you want to debug your application, and find the exact line causing the error you should only mark out the orange line (No need to delete it)

Having the purple line (On Error Resume Next) will eliminate all error traping, as said before.


and as for your specific error caused by the DoCmd.GoToRecord , "", acNext line is probably because you'r trying to move to the next record before you still editing the current one, or you might not have another record
 
and as for your specific error caused by the DoCmd.GoToRecord , "", acNext line is probably because you'r trying to move to the next record before you still editing the current one, or you might not have another record
The error isn't related to the Before Update event or anything to do with editting the record. We've already established this many posts away. If the form is dirty and you click the Next Record button, Before Update will fire and complete before the Next code is fired.

The only time the error will occur (for the Next record is in this scenario):

1. Move to a new record
2. Enter new data
3. Click the Next Record button

... Before Update fires ok, Next code fires but errors because there's no next record.
 
The error isn't related to the Before Update event or anything to do with editting the record. We've already established this many posts away. If the form is dirty and you click the Next Record button, Before Update will fire and complete before the Next code is fired.

The only time the error will occur (for the Next record is in this scenario):

1. Move to a new record
2. Enter new data
3. Click the Next Record button

... Before Update fires ok, Next code fires but errors because there's no next record.
basicely that's what I wrote:
QUOTE or you might not have another record /QUOTE
 
Hi Jr,

You win the prize for coming closest to the answer!!

I tried your suggestion of adding "ClearMacroError" in my original project, and it does work fine when canceling the update of an edited record!

However, it does not pop up a message if you try to go to "Previous" when on the first record, nor "Next" when on the last record.

I'd rather have it this way, though, because the lack of MsgBox makes more sense when cancelling an edit, and it's just quiet when trying to navigate before record one or after the last record.

Next I'm going to try the expanded "If [MacroError] = 2105..." version, abnd see how that works for me.

Thanks!
 
Hi spikepl,

Thanks again for your help.

I just noticed where you're from....With this year's Independence Day being the 45th, there's probably going to be quite the party on the island!

;)
 
Hi jdraw,

Thanks for your response.

Interesting thing, though. I tried to do what you suggested:

Also, not everyone has Acc2007 or 2010, so can not open an accdb.
If you include an mdb version of you database, you may get more responses.
I tried to "Save As" in mdb format, then tried to open the application in Access 2003. Unfortunately, it doesn't translate well, the mdb doesn't work, so there's no point posting it.

Thanks!
 
Hi vbaInet,

Thanks for all of your input!

A couple of things, though:

The only time the error will occur (for the Next record is in this scenario):

1. Move to a new record
2. Enter new data
3. Click the Next Record button

That's not exactly what's happening. The problem is occurring on the first record of a few hundred, not on a new record.

Your converted macro buttons don't error because you have On Error Resume Next so I don't understand why you say it errors. I've just downloaded your test db and it works fine.

Oh-oh. I've tried it many times, and it very consistently fires a runtime error. Are you trying it with the first record? The error happens when you try to do a "previous" when on the first record. The error handler should be taking care of it, but it doesn't.

And I can't take credit for any of the code in the test database. All of it was created by Access! I guess they added an "error wizard"!

Thanks!
 
Hi Smig,

Thanks for the detailed analysis (in color!!) of the error processing.

Two points:

1) DoCmd.GoToRecord , "", acNext line is not raised because there is no next record. I am testing this on the first record of a few hundred in the table, so that's not a problem.

2) The code I posted, and which you quoted, was generated by Access, including the "On Error Resume Next."

I've tested this with/without "On Error GoTo btnNext_Click_Err" and with/without "On Error Resume Next."

Thanks!
 
That's not exactly what's happening. The problem is occurring on the first record of a few hundred, not on a new record.
That is the only scenario it will happen where an Update was fired.

Oh-oh. I've tried it many times, and it very consistently fires a runtime error. Are you trying it with the first record? The error happens when you try to do a "previous" when on the first record. The error handler should be taking care of it, but it doesn't.
Yes I know it has to be on the first record for the Previous button or on a new record for the Next button for the error to occur.

I've tried it again and the error handler works just fine. Perhaps your version of Access is buggy or you haven't Debug > Compiled your code in a long while.
 

Users who are viewing this thread

Back
Top Bottom