Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Handling Errors and Debugging Code - Pg. 172

Using Visual Basic to Automate Your Database 172 This example show the kinds of errors that can occur and the sort of code you can add to a procedure to handle these errors. As you test the code you add to a database, you'll discover situations such as this and can then add the code required to deal with them. Just the Steps Add error-handling code 1. By default, the event procedures you create using a control wizard will contain basic error-handling code. If you're creating a procedure from scratch, enter a statement such as the following after the procedure's declaration: On Error Go To ErrorHandlingLabelName 2. 3. 4. Enter the statements that you want to run when an error occurs. For example, use the MsgBox function to display a message to the user. Include a Resume statement to direct the procedure to the label where execution should resume. Include the Exit Sub statement before the error-handling code so that the code doesn't run when an error doesn't occur. Debugging Tools The Visual Basic Editor's debugging tools provide ways to review your code while you run and test it, which helps you identify problem areas that need fixing. You can step through your code one statement at a time so that you can see which statement causes a problem. You can monitor the values of expressions and variables while a program runs, and you can track procedure calls. Most of the debugging commands appear on the Debug menu and on the Debug toolbar, which you can