We help IT Professionals succeed at work.

On Error Problem... Resume!!!

kennedymr
kennedymr asked
on
I am having a problem with on error, appreciate some simple advice to overcome this.

For the sake of the exercise TTemp2 is locked.


on error goto bad1
Fsys.DeleteFile TTemp2, force
goto allok

bad1: if err.number=70 then
resume
endif

allok:on error goto 0

---

If the file stays locked on error 70, how do i (via a command button??), tell the program to forget the error and get on with the next section of the program.



Appreciate any help
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012

Commented:
Change "resume" to "Resume next"
Anthony

Author

Commented:
I have edited the question a little, sorry anthony., i was not quick enough!!!!
Regards  kennedymr
CERTIFIED EXPERT
Top Expert 2012
Commented:
Here is one approach you can take using your code as much as possible.  (However a better approach would be to dispense with GoTo altogether)

On Error GoTo bad1
Fsys.DeleteFile TTemp2, force
GoTo allok

bad1:
If Err.Number = 70 Then
   If MsgBox("The file is locked", vbRetryCancel) = vbRetry Then
      Resume
   Else
      Resume Next
End If

allok:
On Error GoTo 0

Anthony

Author

Commented:
Thanks for you help

Regards  kennedymr
CERTIFIED EXPERT
Top Expert 2012

Commented:
And now I ws not quick enough.  Here is a better approach, encapsulating the delete functionality in its own function that I think is somewhat clearer:

...
If FileDeleted(Fsys, "c:\some unimportant file.txt") Then

Else

End If
...

Private Function FileDeleted(Fsys As FileSystemObject, ByVal TTemp2 As String) As Boolean

On Error GoTo ErrHandler

Fsys.DeleteFile TTemp2, force
FileDeleted = True

ErrHandler:
If Err.Number = 70 Then
   If MsgBox("The file is locked", vbRetryCancel) = vbRetry Then
      Resume
   Else
      FileDeleted = False
   End If
Else
   ' Unexpected error
   MsgBox Err.Description, vbCritical, "Error: " & CStr(Err.Number)
End If

End Function

CERTIFIED EXPERT
Top Expert 2012

Commented:
Oops! classic mistake forgot the Exit Function:

...
If FileDeleted(Fsys, "c:\some unimportant file.txt") Then

Else

End If
...

Private Function FileDeleted(Fsys As FileSystemObject, ByVal TTemp2 As String) As Boolean

On Error GoTo ErrHandler

Fsys.DeleteFile TTemp2, force
FileDeleted = True

Exit Function

ErrHandler:
If Err.Number = 70 Then
  If MsgBox("The file is locked", vbRetryCancel) = vbRetry Then
     Resume
  Else
     FileDeleted = False
  End If
Else
  ' Unexpected error
  MsgBox Err.Description, vbCritical, "Error: " & CStr(Err.Number)
End If

End Function
CERTIFIED EXPERT
Top Expert 2012

Commented:
And one more detail:
...
If FileDeleted(Fsys, "c:\some unimportant file.txt") Then

Else

End If
...

Private Function FileDeleted(Fsys As FileSystemObject, ByVal TTemp2 As String) As Boolean

On Error GoTo ErrHandler

Fsys.DeleteFile TTemp2, force
FileDeleted = True

Exit Function

ErrHandler:
If Err.Number = 70 Then
 If MsgBox("The file is locked", vbRetryCancel) = vbRetry Then
    Resume
 Else
    FileDeleted = False
 End If
Else
 ' Unexpected error
 MsgBox Err.Description, vbCritical, "Error: " & CStr(Err.Number)
   FileDeleted = False

End If

End Function

Explore More ContentExplore courses, solutions, and other research materials related to this topic.