CodeItBetter Programming Another VB Programming Blog

How to use code that allows the user to abort, retry, or ignore when an error occurs

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'Miscellaneous - How to use code that allows the user to abort, retry, or ignore when an error occurs
Private Sub CheckItOut()
    Dim blnGotFile As Boolean
    Dim strFileName As String
    Dim strTxt As String
 
    blnGotFile = False
    Do Until blnGotFile
        ' Get a file in A:\.
        On Error Resume Next
        strFileName = Dir$("A:\*.*")
        blnGotFile = (Err.Number = 0)
        On Error GoTo 0
 
        ' See if we got a file name.
        If Not blnGotFile 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.
                strFileName = ""
                blnGotFile = True
            End Select
        End If
    Loop
 
    ' See if the user picked Ignore.
    If Len(strFileName) = 0 Then
        ' Ignore. Take a default action.
        strTxt = "Here's the default text."
    Else
        ' Success. List the files in A:\.
        strTxt = "Files:" & vbCrLf
        Do While Len(strFileName) > 0
            strTxt = strTxt & "        " & strFileName & vbCrLf
            strFileName = Dir$()
        Loop
    End If
 
    MsgBox strTxt, vbOKOnly, "Results"
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.