Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse code that allows the user to abort, retry, or ignore when an error occurs
DescriptionThis example shows how to use code that allows the user to abort, retry, or ignore when an error occurs in Visual Basic 6.
Keywordserror, error handling, abort, try, ignore
CategoriesSoftware Engineering
 
When you click this program's List button, the code tries to list the files in floppy drive directory A:\. If the drive is empty, this raises an error. The program displays an error message and lets the user abort (stop trying), retry (the user has inserted a floppy disk), or ignore (move on to something else.

The program enters a loop that lasts until it gets a file name. Inside the loop, the program uses Dir$ to look for a file in A:\. If the program does not get an error, it sets got_file to True to end its loop.

If the Dir$ function raises an error, the program tells the user there is a problem and asks what to do. If the user picks Abort, the program displays a message and exits the subroutine. If the user clicks Retry, then the code continues its loop to try again. If the user clicks Ignore, then the program fills in a default blank file name and sets got_file to True so it exits its loop.

After the loop ends, the program checks the file name to see if it found a file or the user clicked Ignore. If the user clicke Ignore, the program takes some default action. If the program found a file, it lists the files in A:\.

 
Private Sub cmdList_Click()
Dim got_file As Boolean
Dim file_name As String
Dim had_error As Boolean
Dim txt As String

    got_file = False
    Do Until got_file
        ' Get a file in A:\.
        On Error Resume Next
        file_name = Dir$("A:\*.*")
        got_file = (Err.Number = 0)
        On Error GoTo 0

        ' See if we got a file name.
        If Not got_file Then
            ' Trouble. The drive may be empty.
            ' Ask the user what to do.
            Select Case MsgBox("Error reading A drive. " & _
                    Err.Description & vbCrLf & "Try " & _
                        "again?", _
                    vbAbortRetryIgnore, "Error Reading " & _
                        "Drive")
                Case vbAbort
                    ' Cancel the operation.
                    MsgBox "Operation canceled"
                    Exit Sub
                Case vbRetry
                    ' Continue the Do loop to try again.
                Case vbIgnore
                    ' Take a default action.
                    file_name = ""
                    got_file = True
            End Select
        End If
    Loop

    ' See if the user picked Ignore.
    If Len(file_name) = 0 Then
        ' Ignore. Take a default action.
        txt = "Here's the default text."
    Else
        ' Success. List the files in A:\.
        txt = "Files:" & vbCrLf
        Do While Len(file_name) > 0
            txt = txt & "        " & file_name & vbCrLf
            file_name = Dir$()
        Loop
    End If

    MsgBox txt, vbOKOnly, "Results"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated