Today’s guest blogger is Michael Groh, co-author of the popular Access 2007 Bible. Most of us, when faced with a failure in a large procedure, resort to the usual techniques for discovering the cause of the bug. The scenario here is that you've got some good VBA error handling in the procedure, and it's bombing out with a hard-to-find divide by zero error. Here's an example of such a procedure. Just pretend it contains several hundred lines of code and not just the few you see here: Public Function MyBrokenFunction(sng1 As Single, sng2 As Single) As Single Dim X As Single On Error GoTo HandleError X = sng1 / (sng2 - 7654321) X = sng1 / (sng2 - 8765421) >> More code here X = sng1 / (sng2 - 9876543) X = sng1 / (sng2 - 1234567) >> And here X = sng1 / (sng2 - 2345678) >> etcetera X = sng1 / (sng2 - 3456789) MyBrokenFunction = x ExitHere: Exit Function HandleError: MsgBox("Error: " & Err.Number & " " & Err.Description) Resume ExitHere End Function So far, so good, right? The error handler is working as expected,
Buy Office Professional Plus 2010, but you don't know for sure which of the many division statements is causing the divide-by-zero error. The usual process at this point is to do one or all of the following: Add line numbers to the function, and get the errant line number with the Erl function (Luke Chung has a great article on error handling at Manually check each and every mathematical operation (pretty difficult to do,
Windows 7 Ultimate 64, given that variables are used in this function's statements). Disable the "On Error GoTo" by commenting it out,
Office 2010 Serial Generator, and run the function again. It'll crash right on the errant statement because the error isn't handled at this point. But, here's the clever way to handle this situation. When you run the app and the error occurs,
Buy Office Home And Student 2010, press Ctrl+Break instead of clicking the OK button on the message box. Access displays a "Code execution has been interrupted" message, and opens the VBA editor on the Resume ExitHere statement. (It opens on this statement because the MsgBox statement in the error handler has already executed, and VBA is just about to execute the Resume ExitHere statement.) Add a comment character just before ExitHere so the statement reads: Resume 'ExitHere and press F8. Because you're using the F8 key to single-step through the code, only the Resume statement executes and the pointer is placed on the statement. (The Resume statement instructs VBA to go back and repeat the errant statement.) Fix the problem,
Windows 7 Home Basic Product Key, and remove the comment from within the Resume ExitHere statement, and you're good to go. <div