vba 链接表和 Ms Access 错误(运行时错误“3622”:dbSeeChanges/Identity 列)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17165332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:43:20  来源:igfitidea点击:

Errors with linked tables and Ms Access ( Run-time error '3622' : dbSeeChanges/Identity column )

sqlsql-servervbams-access

提问by Anton Hughes

I am trying to output the name of all linked tables, including their fields which are Date/Time, and that fields values.

我试图输出所有链接表的名称,包括它们的日期/时间字段和字段值。

The following code can output the first table, field name and their first value, not all values, although when it gets to the next linked table, I get this error

下面的代码可以输出第一个表,字段名和它们的第一个值,不是所有的值,虽然当它到达下一个链接表时,我得到这个错误

Run-time Error '3622' 
You must use the dbSeeChanges option with OpenRecordSet when accessing a SQL Server table that has an IDENTITY column.

Here is my code

这是我的代码

Private Sub btnGetFields_Click()

   Dim db As DAO.Database
   Dim tdf As DAO.TableDef
   Dim f As Field
   Dim rst As DAO.Recordset
   Dim numField As Integer

   Set db = CurrentDb

   For Each tdf In db.TableDefs

        If Left$(tdf.Connect, 9) = "ODBC;DSN=" Then

            Set rst = CurrentDb.OpenRecordset(tdf.Name)
            numField = rst.Fields.Count

            Debug.Print "Table: " & tdf.Name
            For index = 0 To numField - 1
                 If rst.Fields(index).Type = dbDate Then

                     Debug.Print "Field: " & rst.Fields(index).Name; "   Value : "; rst.Fields(index).Value
                 End If
            Next



        End If

   Next

   Set tdf = Nothing
   Set db = Nothing

End Sub

I read something that if I'm using sql tables I should use ADO? Any ideas?

我读过一些内容,如果我使用 sql 表,我应该使用 ADO?有任何想法吗?

回答by Gord Thompson

You can continue to use your existing DAO code, just change

您可以继续使用您现有的 DAO 代码,只需更改

Set rst = CurrentDb.OpenRecordset(tdf.Name)

to

Set rst = CurrentDb.OpenRecordset(tdf.Name, dbOpenSnapshot)

That opens a static read-only Recordset, so dbSeeChangesis not required.

这将打开一个静态只读 Recordset,因此dbSeeChanges不是必需的。