As you add error handlers to your applications, you'll soon discover that you're handling the same errors over and over again. You can reduce code size and save yourself time and effort by writing a few procedures that your error handlers can call to handle common error situations.
The following code is a function called FileErrors that can be called by an error handler. This function displays a message corresponding to the error that has occurred and, where possible, allows the user to choose a button to specify which action the function should take next. It then returns a code number to the procedure that called it (the calling procedure isn't shown in the example). The code number indicates which action — out of a set of actions — the program should take.
Public Const RESUME_STATEMENT = 0 'Resume Public Const RESUME_NEXT = 1 'Resume Next Public Const UNRECOVERABLE = 2 'Unrecoverable error Public Const UNRECOGNIZED = 3 'Unrecognized error Public Const Err_DeviceUnavailable = 68 Public Const Err_BadFilenameOrNumber = 52, Err_PathDoesNotExist = 76 Public Const ERR_BADFILEMODE = 54 Function FileErrors (errVal As Integer) As Integer Dim MsgType As Integer, Msg As String, Response As Integer MsgType = vbExclamation Select Case errVal Case Err_DeviceUnavailable ' Error #68 Msg = "That device appears unavailable." MsgType = MsgType + vbAbortRetryIgnore Case Err_BadFilenameOrNumber ' Errors #52 Msg = "That filename is not valid." MsgType = MsgType + vbOKCancel Case Err_PathDoesNotExist ' Error #76 Msg = "That path doesn't exist." MsgType = MsgType + vbOKCancel Case Err_BadFileMode ' Error #54 Msg = "Can't open your file for that type of access." MsgType = MsgType + vbOKCancel Case Else FileErrors = UNRECOGNIZED Exit Function End Select Response = MsgBox(Msg, MsgType, "Disk Error") Select Case response Case vbOK, vbRetry FileErrors = RESUME_STATEMENT Case vbIgnore FileErrors = RESUME_NEXT Case vbCancel, vbAbort FileErrors = UNRECOVERABLE Case Else FileErrors = UNRECOGNIZED End Select
End Function
The function in the preceding example handles a select group of file-related and disk-related errors. If an error you encounter doesn't belong to this set of errors, the function returns the value 3. The procedure that calls this function should then either handle the error itself, call another procedure to handle it, or just let the original run-time error occur again, as shown in the following example.
Function FileOperationsDemo(FileName As String) 'Demo of how you would call the FileErrors Function On Error GoTo ConfirmFileError . . . 'Code that attempts some file operation . . . ConfirmFileError: Action = FileErrors(Err) 'Call the FileErrors function Select Case Action Case RESUME_STATEMENT 'User chose OK or Retry when FileErrors 'displayed its message, so try again. Resume Case RESUME_NEXT 'User chose Ignore when FileErrors 'displayed its message, so ignore error 'and continue. Resume Next Case UNRECOVERABLE 'User chose Cancel or Abort when 'FileErrors displayed its message, 'so abort procedure. Exit Function Case Else 'Cannot handle original run-time 'error, so repeat it and display 'the normal System error. Error Err End Select End Function