Access 2007 DAO VBA Error 3381 causes objects in calling methods to “break”
-
19-09-2019 - |
Question
---AFTER FURTHER INVESTIGATION--- "tblABC" in the below example must be a linked table (to another Access database).
If "tblABC" is in the same database as the code then the problem does not occur.
Hi,
We have recently upgraded to Office 2007.
We have a method in which we have an open recordset (DAO). We then call another sub (UpdatingSub below) that executes SQL. This method has its own error handler. If error 3381 is encountered then the recordset in the calling method becomes "unset" and we get error 3420 'Object invalid or no longer set'. Other errors in UpdatingSub do not cause the same problem.
This code works fine in Access 2003.
Private Sub Whatonearth()
Dim rs As dao.Recordset
set rs = CurrentDb.OpenRecordset("tblLinkedABC")
Debug.Print rs.RecordCount
UpdatingSub "ALTER TABLE tblTest DROP Column ColumnNotThere"
'Error 3240 occurs on the below line even though err 3381 is trapped in the calling procedure
'This appears to be because error 3381 is encountered when calling UpdatingSub above
Debug.Print rs.RecordCount
End Sub
Private Sub WhatonearthThatWorks()
Dim rs As dao.Recordset
set rs = CurrentDb.OpenRecordset("tblLinkedABC")
Debug.Print rs.RecordCount
'Change the update to generate a different error
UpdatingSub "NONSENSE SQL STATEMENT"
'Error is trapped in UpdatingSub. Next line works fine.
Debug.Print rs.RecordCount
End Sub
Private Sub UpdatingSub(strSQL As String)
On Error GoTo ErrHandler:
CurrentDb.Execute strSQL
ErrHandler:
'LogError'
End Sub
Any thoughts? We are running Office Access 2007 (12.0.6211.1000) SP1 MSO (12.0.6425.1000). Perhaps see if SP2 can be distributed?
Sorry about formatting - not sure how to fix that.
Solution 2
Thanks for all your input - and sorry for any confusion re my problem - it was a weird one. I tried it on a PC running
Office Access 2007 (12.0.6423.1000) SP2 MSO (12.0.6425.1000)
rather than
Office Access 2007 (12.0.6211.1000) SP1 MSO (12.0.6425.1000)
and I do not get the same problem. Time to see if we can get the powers to be to install the newer version.
OTHER TIPS
That error indicates that there is no such column in the table. The code above can only be run once. You may wish to check that the column (field) exists before you delete it.
Edited after comment:
Private Sub Whatonearth()
Dim rs As DAO.Recordset
strColName = "ColumnNotThere"
Set rs = CurrentDb.OpenRecordset("tblABC")
For Each fld In rs.Fields
If fld.Name = strColName Then
Debug.Print rs.RecordCount
''The recordset will have to be closed
''before calling UpdatingSub
rs.Close
UpdatingSub "ALTER TABLE tblABC DROP Column " & strColName
''Debug.Print rs.RecordCount
Exit For
End If
Next
End Sub
''To get a proper error with SQL, you need dbFailOnError
''You may also need to loop through the errors collection*
Private Sub UpdatingSub(strSQL As String)
On Error GoTo ErrHandler
CurrentDb.Execute strSQL, dbFailOnError
ErrHandler:
''LogError
Debug.Print Err.Description
End Sub
'' Enumerate Errors collection and display properties of
'' each Error object.
For Each errLoop In Errors
With errLoop
strError = _
"Error #" & .Number & vbCrLf
strError = strError & _
" " & .Description & vbCrLf
strError = strError & _
" (Source: " & .Source & ")" & vbCrLf
End With
I'm glad that someone else has also encountered this problem. I had exactly the same issue (error 3381 causing problems with DAO recordset), and also with pass through queries running SQL statements on SQL Server 2000.
Once I prevented the error 3381 from occuring (by checking for the existence of the field before trying to DROp it from a table), no further problems.
For what it's worth, I was running Office Access 2007 (12.0.6211.1000) SP1 MSO (12.0.6320.5000).
Definitely seems like an Access DAO issue that Microsoft may have resolved with later Service Pack.