Re: Infinite Loop in ErrorHandler That Requires Hard Shutdown



"robert d via AccessMonster.com" <u6836@uwe> wrote in message
5a6c77625c7b2@uwe">news:5a6c77625c7b2@uwe
> I use unbound forms and DAO. I followed the recommendations of some
> others and have come up with the following Exit Handler and Error
> Handler (this is just an example; rstFlag states whether the
> recordset is open and bInTrans states whether BeginTrans is turned on)
>
> On Error GoTo Err_Ctrl
>
> Dim wst As DAO.Workspace
> Dim dbt As DAO.Database
> Dim rst As DAO.Recordset
>
> Exit_Sub:
>
> If rstFlag = True Then
> rst.Close
> Set rst = Nothing
> dbt.Close
> Set dbt = Nothing
>
> If bInTrans Then 'Rollback if the transaction is active.
> wst.Rollback
> End If
>
> Set wst = Nothing
> rstFlag = False
> End If
>
> DoCmd.SetWarnings True
> Exit Sub
>
> Err_Ctrl:
> DoCmd.Hourglass False
> errMsgStr = ""
> ctrlfnctnm = "Save_Edit_Button"
> 'Call global error handler
> Call BuyInfoForm_err(Err.Number, Err.Description, Err.Source,
> ctrlfnctnm, errMsgStr)
> Resume Exit_Sub
>
> I recently added the transaction conditional statement in the exit
> handler. I had a problem with the code where I began the transaction
> but then forgot to commit or roll it back in the body of the code.
> When the code got to rst. Close in the Exit handler, I got an error.
> Okay, I fixed the error, but the problem with the error was that it
> created an infinite loop. The Exit Handler error called the Error
> Handler which then tried to call the Exit Handler which then called
> the Error Handler and so on.
>
> I thought I was handling my Error Handling and Exit Handling
> correctly. But I see now that if there is an error in the Exit
> Handler it creates an infinite loop.
>
> What am I doing wrong?
>
> Thanks.

You should either ignore errors once you reach the exit code, like this:

Exit_Sub:
On Error Resume Next

or else set a flag that indicates where you are in the procedure -- for
example, that you are in the exit code -- and let your error handler
check that flag to determine where to Resume execution.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.