Ms access vba error resume

ms access vba error resume

The "Resume" and "GoTo" statements simply tell VBA what to do when an error is encountered. First, Resume has two primary actions associated with it. This tutorial will demonstrate how to use the VBA On Error Resume Next and On Error Goto 0. Read our Error Handling Tutorial for more information about VBA. › watch.

Ms access vba error resume - are


For a task where several things could go wrong, lines 7~8 will be replaced with more detail:

Select Case Case Resume Next Case Resume Exit_SomeName Case Else Call LogError(, sprers.euption, "SomeName()") Resume Exit_SomeName End Select

The Case Else in this example calls a custom function to write the error details to a table. This allows you to review the details after the error has been cleared. The table might be named "tLogError" and consist of:

Field NameData TypeDescription
ErrorLogIDAutoNumberPrimary Key.
ErrNumberNumberLong Integer. The Access-generated error number.
ErrDescriptionTextSize= The Access-generated error message.
ErrDateDate/TimeSystem Date and Time of error. Default: =Now()
CallingProcTextName of procedure that called LogError()
UserNameTextName of User.
ShowUserYes/NoWhether error data was displayed in MsgBox
ParametersText Optional. Any parameters you wish to record.


Below is a procedure for writing to this table. It optionally allows recording the value of any variables/parameters at the time the error occurred. You can also opt to suppress the display of information about the error.

Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _ strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean On Error GoTo Err_LogError Dim strMsg As String Dim rst As sprers.euset Select Case lngErrNumber Case 0 strCallingProc & " called error 0." Case Case , , If bShowUser Then strMsg = "Record cannot be saved at this time." & vbCrLf & _ "Complete the entry, or press <Esc> to undo." MsgBox strMsg, vbExclamation, strCallingProc End If Case Else If bShowUser Then strMsg = "Error " & lngErrNumber & ": " & strErrDescription MsgBox strMsg, vbExclamation, strCallingProc End If Set rst = sprers.eucordset("tLogError", , dbAppendOnly) rst![ErrNumber] = lngErrNumber rst![ErrDescription] = Left$(strErrDescription, ) rst![ErrDate] = Now() rst![CallingProc] = strCallingProc rst![UserName] = CurrentUser() rst![ShowUser] = bShowUser If Not IsMissing(vParameters) Then rst![Parameters] = Left(vParameters, ) End If LogError = True End Select Exit_LogError: Set rst = Nothing Exit Function Err_LogError: strMsg = "An unexpected situation arose in your program." & vbCrLf & _ "Please write down the following details:" & vbCrLf & vbCrLf & _ "Calling Proc: " & strCallingProc & vbCrLf & _ "Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _ "Unable to record because Error " & & vbCrLf & sprers.euption MsgBox strMsg, vbCritical, "LogError()" Resume Exit_LogError End Function

Notes on this function:

  1. For Access 1 or 2, use the Access Basic error handler.
  2. The Return Value serves only to indicate if the function succeeded in logging the error.
  3. Possible Extensions: Since you have tErrorLog open, you could count errors recorded recently and suppress the display of the same message repeatedly, or give up retrying locking errors.