I have a project with a number of instances of
DoCmd.Hourglass -1
<various code>
DoCmd.Hourglass 0
<other code>
Any time there is an error, the line turning off the hourglass gets skipped. The users get confused and wait for the app to be ready again.
Normally, I'd handle this in a try..finally
try {
DoCmd.Hourglass -1
<various code>
} finally {
DoCmd.Hourglass 0
}
but that isn't an option in VBA.
I thought it would be a simple matter to search and find the correct approach, but every sample I found takes the naïve approach above.
The trick is that I don't want to affect any of the existing program flow, which means that I need to rethrow the error.
And, of course, "throw" doesn't exist either.
But it also means that I do NOT need to resume where the error happened.
Here's my current best guess
On Error GoTo ErrHandler
DoCmd.Hourglass -1
<various code>
DoCmd.Hourglass 0
<other code>
Exit Function
ErrHandler:
DoCmd.Hourglass 0
Err.Raise Err.Number
End Function
but I have 0 confidence that this is the best solution.
CodePudding user response:
Looks like the question is answered here:
https://stackoverflow.com/a/64478691/1513027
err.raise Err.Number
This is the correct method, and no, you don't have to re-state all the err properties. The values are retained.