Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Using Resume In Error Handlers

When you’re writing code for an error handler, you can return control to the main body of the procedure using the Resume statement. Program execution starts again with the line that caused the error, and this can be very valuable if you’re able to fix the error in the error handler.

Let’s see an example. In this example, we open a file that the user has selected with an Open Common Dialog. If the user clicked the Cancel button instead, we can insist that the user select a file by displaying a message box with the text “Please select a file” and use the Resume statement to display the Open dialog box once again. We do that by trapping the error generated when the user clicks the Cancel button in the Open dialog box (set the Common Dialog control’s CancelError property to True to make sure a trappable error is generated when the user clicks the Cancel button):

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With

    Exit Sub

FileError:

   Select Case Err.Number
       Case cdlCancel
           MsgBox "Please select a file."
           Resume
       Case 53
           MsgBox "File not found"
       Case Default
           MsgBox "File Error"
   End Select

End Sub

Using Resume Label In Error Handlers

When you’re writing code for an error handler, you can return control to a particular line in the main body of the procedure using the Resume Label statement. To label a line, you just place the label’s text directly into the code, followed by a colon.

Let’s see an example. Here, we use the Resume Label statement to retry a File Open operation if the user clicked the Cancel button in the Open dialog box. We do this by using the label TryAgain (to make the Open Common Dialog return a trappable error if the user clicks the Cancel button, set the Common Dialog control’s CancelError property to True):

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1
TryAgain:
        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)
        Close #1

    End With

    Exit Sub

FileError:

   Select Case Err.Number
       Case cdlCancel
           MsgBox "Please select a file."
           Resume TryAgain
       Case 53
           MsgBox "File not found"
       Case Default
           MsgBox "File Error"
   End Select

End Sub

Using Resume Label is useful if you’re able to fix a trappable error in an error handler and want to resume execution at some specific line in the code (not necessarily the next line in the code).

Using Resume line# In Error Handlers

You can use the Resume statement (see the previous two topics) with an actual line number in Visual Basic. For example, here’s how we’d write the example from the previous topic using line numbers instead of a Resume Label statement:

Private Sub Command1_Click()
1
2    On Error GoTo 16
3
4    With CommonDialog1
5
6        .ShowOpen
7        Open .FileName For Input As #1
8        Text1.Text = Input$(LOF(1), #1)
9
10        Close #1
11
12    End With
13
14    Exit Sub
15
16
17
18   Select Case Err.Number
       Case cdlCancel
           MsgBox "Please select a file."
           Resume 6
       Case 53
           MsgBox "File not found"
       Case Default
           MsgBox "File Error"
   End Select

End Sub

In this case, we’re specifying that execution should continue starting at line 6 after we’ve indicated to the users that they should select a file to open instead of just clicking the Cancel button.

Using Resume Next In Error Handlers

Besides Resume, Resume Label, and Resume line#, you can also use the Resume Next statement in an error handler. This statement resumes program execution in the line after the one that caused the error.

Let’s see an example. Here we set the Text, Caption, Min, and Max properties of the currently active control on a form when the user clicks that form. Because no one control has all those properties, this code would generate an error in a message box to the user:

Private Sub Form_Click()

    ActiveControl.Text = "Active control"
    ActiveControl.Caption = "Active Control"
    ActiveControl.Min = 0
    ActiveControl.Max = 100
End Sub

We can handle and suppress these errors with an error handler, which we start at the label SetError:

Private Sub Form_Click()
    On Error GoTo SetError
    ActiveControl.Text = "Active control"
    ActiveControl.Caption = "Active Control"
    ActiveControl.Min = 0
    ActiveControl.Max = 100

    Exit Sub

SetError:
...

In this case, we handle the error by moving on to the next line of code like this with Resume Next:

Private Sub Form_Click()
    On Error GoTo SetError
    ActiveControl.Text = “Active control”
    ActiveControl.Caption = “Active Control”
    ActiveControl.Min = 0
    ActiveControl.Max = 100

    Exit Sub

SetError:
    Resume Next
End Sub

The result here is that whichever of these four properties the active control does have is set, without any errors.


WARNING!  Note, however, that code like this is not good programming form, of course; it’s better to check what the type of the active control is before setting its properties instead of simply disregarding errors.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.