You have a couple of options. First, you don't have to use any If statment at all.This will trap and log the error. Note, no code after the error will be executed.VB Code:
Private Sub Command1_Click() On Error Goto ErrTrap 'code for click event Exit Sub ErrTrap: 'use eusty's example Open "c:\ErrorLog.txt" For Append As #1 Print #1, Format(Now, "mm/dd/yyyy") & " " & Err.Number & " " & Err.Description Close #1 End Sub
If you want to trap for a specific error, as well as all other errors, then you can use the If statementAgain, no code will be executed after the line the error occurred on. If you want to trap and log the error, and continue code execution, then in your error trap, log the error, then add a Resume Next, which will continue executing the code after the line on which the error has occured.VB Code:
Private Sub Command1_Click() On Error Goto ErrTrap 'code for click event Exit Sub ErrTrap: If Err.Number = 3021 Then 'do something for this specific error Else 'do something else regardless of what the error is End If End Sub




Reply With Quote