VBA error handler, how to not close the form (1 Viewer)

dhop1990

Registered User.
Local time
Today, 17:18
Joined
Oct 25, 2013
Messages
20
I have a module named modErrorHandler that has the below error handler. It save the error successfully to my table named tblLogErrors. But the the only problem is that on my On_click event to close the form still executes and then presents the error message.

I had another database that I practiced putting this code in, albeit on After Update event.

Here is the code for the Click Event
Code:
Private Sub cmdOpenCharts_Click()
On Error GoTo Err_Proc
'When there is an error jump to Proc_Err
        DoCmd.Close
        strFormName = "frmCharts"
        DoCmd.OpenForm (strFormedName)
        
Exit_Proc:
    Exit Sub
Err_Proc:
'Calls LogError function from modErrorHandler and passes the Error number,
'description and procedure name to function LogError.
    Call LogError(Err.Number, Err.Description, "cmdOpenCharts_Click()")
    Resume Exit_Proc
    'To leave  the program we must resume after this message by moving to Exit
End Sub

Here is the code for the Function stored in a module
Code:
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 DAO.Recordset    
    Select Case lngErrNumber
    Case 0
        Debug.Print strCallingProc & " called error 0."   
    Case 2501       
    Case 3314, 2101, 2115  
        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 = CurrentDb.OpenRecordset("tblLogError", , dbAppendOnly)
        rst.AddNew
            rst![ErrNumber] = lngErrNumber
            rst![ErrDescription] = Left$(strErrDescription, 255)
            rst![ErrDate] = Now()
            rst![CallingProc] = strCallingProc
            rst![UserName] = CurrentUser()
            rst![ShowUser] = bShowUser
           
            If Not IsMissing(vParameters) Then
                rst![Parameters] = Left(vParameters, 255)
            End If
            rst.Update
            rst.Close 
            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 " & Err.Number & vrCrLf & Err.Description
    MsgBox strMsg, vbCritical, "LogError()"
    Resume Exit_LogError
       
End Function
Code:
Public strFormName As String


Any help would be greatly appreciated. I'm not sure what I'm doing wrong.

I've tried playing around with On Error Resume Next. I guess I need something to cancel out the events in the code above?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:18
Joined
Aug 30, 2003
Messages
36,118
Is this a test and you're purposely raising an error with the bad variable name? ;)

You could move the close to the end.
 

dhop1990

Registered User.
Local time
Today, 17:18
Joined
Oct 25, 2013
Messages
20
Is this a test and you're purposely raising an error with the bad variable name? ;)

You could move the close to the end.

Yea I left strFormName as strFormedName. :D At least I hope that's what you are referring to.:confused:

If I move the Close to the end then it just closes the newly opened form when correct. I guess I should specify what form to close?

Edit: Did it and fine now, thanks for the suggestion. I guess I can mark this as solved. I feel so silly right now
 
Last edited:

Users who are viewing this thread

Top Bottom