VBA Programmatically Break on All Errors

2019-07-23 13:16发布

What I want to do is set a flag to go from this option: enter image description here

To this option:

enter image description here

The reason for this is that I have error handlers throughout my code and whilst I'm debugging I want to be able to see what the errors are.

Setting the error flag off:

On Error GoTo 0

Will just take me back to the last place error handling was used, like two or three procedures before the one I'm working on

2条回答
倾城 Initia
2楼-- · 2019-07-23 13:48

You didn't say which VBA environment you are using. In Access (and only in Access, as far as I know), there is a very useful method to programmatically modify the Error Trapping setting:

Application.SetOption("Error Trapping", 2) 
Debug.Print Application.GetOption("Error Trapping") 

I've used this many times where I have error handling set up on an outer method, but I really want to see where the code is hitting an error somewhere far down in a call stack. And just globally setting the Error Trapping to "Break on all errors" is a pain if you have expected, well-handled errors somewhere in your code, because it always stops on those! So, in Access, you can switch to "Break on All Errors" just in one section when you want to.

查看更多
时光不老,我们不散
3楼-- · 2019-07-23 13:50

As per the comments, there isn't an easy way to do this other than manually change the setting in the options when you get to the section of the code you want to have a runtime error for rather than being taken to the last active error handler.

查看更多
登录 后发表回答