I'm been struggling with a mysterious problem for days now and I finally found the cause. Now I wonder if anyone knows how to fix it...
Here's the problem: I open frmCashAccounts, which is bound to a query that draws from tblAccounts. The form has a standard "Go to record" combo box with automatically generated code behind it (there's other code behind the combo as well, but this is the part that gives the error):
frmCashAccounts has a subform which displays transactions based on the main form's AcctID. Clicking "Edit" on a record in the subform calls up a separate form (frmTransaction) to edit that transaction. After making changes to the transaction, the user clicks "OK" and then frmTransaction makes changes to tblTransactions and tblSplits using DAO and then closes, returning focus to frmCashAccounts. Here's some of the code:
Then when I return to frmCashAccounts and try to change the current record using the "Go to record" combo box, I get run-time error 3420:
If I edit frmTransaction and remove all code related to wks (setting db to CurrentDb instead) the problem goes away.
So it seems that declaring a Workspace and setting it to DBEngine(0) and then closing it causes frmCashAccounts to lose track of its recordset. Is this expected behavior? Is there any way around it?
I'd really like to be able to rollback on error, and using a Workspace is the only way I know how to do it. Anyone have any suggestions?
Thanks in advance!
Here's the problem: I open frmCashAccounts, which is bound to a query that draws from tblAccounts. The form has a standard "Go to record" combo box with automatically generated code behind it (there's other code behind the combo as well, but this is the part that gives the error):
Code:
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[AcctID] = " & Str(Nz(Me![cboGoTo], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
Code:
Dim wks As Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
...
Set wks = DBEngine(0)
Set db = wks.Databases(0)
wks.BeginTrans
' set recordset using db.OpenRecordset and make various changes
...
wks.CommitTrans dbForceOSFlush
...
PROC_EXIT:
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
wks.Close
Set wks = Nothing
Exit Function
PROC_ERR:
wks.Rollback
The debugger highlights the following code:Run-time error '3420': Object invalid or no longer set.
Code:
Set rs = Me.Recordset.Clone
So it seems that declaring a Workspace and setting it to DBEngine(0) and then closing it causes frmCashAccounts to lose track of its recordset. Is this expected behavior? Is there any way around it?
I'd really like to be able to rollback on error, and using a Workspace is the only way I know how to do it. Anyone have any suggestions?
Thanks in advance!