D
Drahala via AccessMonster.com
I am experiencing a problem where a DoCmd.close command that has been coded
into a While Not loop does not terminate the execution of code where it
should.
Background:
I have created a custom progress bar on a form (Access 2003) that keeps track
of records that are being either archived or deleted, by way of a WHILE NOT
END OF FILE loop. The progress bar has a "Cancel" button on it which when
clicked, sets the value of a boolean variable named booCancel to "True". On
each iteration of the loop, the code checks the value of booCancel, and if
true records the cancel event to an audit log, closes and destroys the DAO
record set and closes the form.
Sample Code:
'Loop through records
While Not rs.EOF
'Check if process is canceled
If booCancel = True Then
Select Case intChoice
Case 1
Call AuditLog("Archive Cancel")
Case 2
Call AuditLog("Deletion Cancel")
End Select
rs.Close
Set rs = Nothing
DoCmd.Close
End If
rs.Edit
Problem:
The loop works as intended until the user clicks the Cancel button. When
this happens, the code executes all the code within the If statement
(including the DoCmd.close statement), and continues on to the rs.Edit
statement, which causes an "Error 91, Object Variable or With Block Variable
Not Set" error to occur as there is no record set to edit because the code
closed it.
An interesting point to note is that this error only occurs during run time.
When I step through the code in debug mode, the code executes perfectly; the
audit log is updated and the form closes without error.
I was under the impression that once DoCmd.close was called, all form level
code execution stopped, but this is apprently not the case here. Any insight
that can be offered here would be greatly appreciated.
into a While Not loop does not terminate the execution of code where it
should.
Background:
I have created a custom progress bar on a form (Access 2003) that keeps track
of records that are being either archived or deleted, by way of a WHILE NOT
END OF FILE loop. The progress bar has a "Cancel" button on it which when
clicked, sets the value of a boolean variable named booCancel to "True". On
each iteration of the loop, the code checks the value of booCancel, and if
true records the cancel event to an audit log, closes and destroys the DAO
record set and closes the form.
Sample Code:
'Loop through records
While Not rs.EOF
'Check if process is canceled
If booCancel = True Then
Select Case intChoice
Case 1
Call AuditLog("Archive Cancel")
Case 2
Call AuditLog("Deletion Cancel")
End Select
rs.Close
Set rs = Nothing
DoCmd.Close
End If
rs.Edit
Problem:
The loop works as intended until the user clicks the Cancel button. When
this happens, the code executes all the code within the If statement
(including the DoCmd.close statement), and continues on to the rs.Edit
statement, which causes an "Error 91, Object Variable or With Block Variable
Not Set" error to occur as there is no record set to edit because the code
closed it.
An interesting point to note is that this error only occurs during run time.
When I step through the code in debug mode, the code executes perfectly; the
audit log is updated and the form closes without error.
I was under the impression that once DoCmd.close was called, all form level
code execution stopped, but this is apprently not the case here. Any insight
that can be offered here would be greatly appreciated.