VBA Error Handling

just a simple example

Sub StartPROC()

    Debug.Print „start“ ‚ write to immediate
    Dim Con As ADODB.Connection, SQL As String

    Set Con = New ADODB.Connection
    SQL = „Testdb.scripts“
    On Error GoTo errHandler
    Con.ConnectionString = „DSN=Testdb;Uid=Test;Pwd=test123;“
    Con.Execute (SQL)
    Set Con = Nothing
    Debug.Print „end“ ‚ write to immediate

    ‚ do other clean up here

    Exit Sub ‚important to exit sub before errHandler
    Dim ShortErr, ErrorPath As String
    ErrorPath = „StartPROC() „
    ShortErr = Left(Err.Description, 150 – Len(ErrorPath))
    Debug.Print „print: “ & Err.Description & “ “ & Err.Number & “ “ & Err.Source ‚ write to immediate
Resume DoCleanUp

End Sub

more examples here and an explanation of all error handling methods here