1

---进一步调查后---下例中的“tblABC”必须是一个链接表(到另一个 Access 数据库)。

如果“tblABC”与代码在同一个数据库中,则不会出现问题。

你好,

我们最近升级到 Office 2007。

我们有一种方法,其中我们有一个开放的记录集 (DAO)。然后我们调用另一个执行 SQL 的 sub(下面的 UpdatingSub)。此方法有自己的错误处理程序。如果遇到错误 3381,则调用方法中的记录集将变为“未设置”,我们会收到错误 3420“对象无效或不再设置”。UpdatingSub 中的其他错误不会导致相同的问题。

此代码在 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

有什么想法吗?我们正在运行 Office Access 2007 (12.0.6211.1000) SP1 MSO (12.0.6425.1000)。也许看看SP2是否可以分发?

抱歉格式化 - 不知道如何解决这个问题。

4

3 回答 3

1

该错误表明表中没有这样的列。上面的代码只能运行一次。您可能希望在删除之前检查列(字段)是否存在。

评论后编辑:

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
于 2009-07-27T14:27:09.253 回答
0

感谢您的所有输入-对于我的问题的任何困惑,我们深表歉意-这是一个奇怪的问题。我在运行的 PC 上尝试过

Office 访问 2007 (12.0.6423.1000) SP2 MSO (12.0.6425.1000)

而不是

Office 访问 2007 (12.0.6211.1000) SP1 MSO (12.0.6425.1000)

我没有遇到同样的问题。是时候看看我们是否能够获得安装更新版本的权力。

于 2009-07-28T08:30:04.783 回答
0

我很高兴其他人也遇到了这个问题。我遇到了完全相同的问题(错误 3381 导致 DAO 记录集出现问题),并且还通过在 SQL Server 2000 上运行 SQL 语句的查询。

一旦我阻止了错误 3381 的发生(通过在尝试从表中删除它之前检查该字段的存在),就没有进一步的问题了。

值得一提的是,我运行的是 Office Access 2007 (12.0.6211.1000) SP1 MSO (12.0.6320.5000)。

绝对看起来像是 Microsoft 可能已通过更高版本的 Service Pack 解决的 Access DAO 问题。

于 2010-04-09T14:20:01.813 回答