views:

609

answers:

3

---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.

+1  A: 

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
Remou
Absolutely. Sorry - should have mentioned that I am not worried about avoiding the error, but rather why the error handled in "UpdatingSub" has any effect on the Recordset object in the calling method "Whatonearth". It shouldn't so I am wondering if this is a bug in the version of Acces/VBA that I am using...
MT
I don't quite get what you mean. If a procedure calls another procedure, the first procedure will be affected by errors in the called procedure. It is usual to comment out error handling when tracking down errors. I have added a few notes that may help.
Remou
Thanks for your post. Looks like my question is a little unclear. I have tried to edit it to make it more clear. Basically if error 3381 occurs in "UpdatingSub" then unexpected errors start occuring in the calling method. If an error other than 3381 occurs then the calling method can carry on as normal... weird
MT
Is there a reason you can't post the code in UpdatingSub?
Kyralessa
That is all the code....shame you can't post files on here.
MT
A: 

Hi All,

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.

MT
A: 

Hi,

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.

MattyP