运行-time error '3326' 此 Recordset 不可更新

Run-time error '3326' This Recordset is not updateable

我有一个使用 Access 2007 创建的应用程序,它与 DSN 完美配合,我更新应用程序以使用无 DSN 连接到 SQL-Server,我没有更改任何代码应用程序,但是当我 运行 应用程序时,我收到此错误

Run-time error '3326' This Recordset is not updateable

Sub DnsLessLinkTable()

    Dim td As TableDef
    Dim stConnect As String
    Dim strTablesName(0 To 7) As String
    strTablesName(0) = "dbo_Directorate"
    strTablesName(1) = "dbo_Nationality"
    strTablesName(2) = "dbo_personal"
    strTablesName(3) = "dbo_Qualification"
    strTablesName(4) = "dbo_Qualimain"
    strTablesName(5) = "dbo_Qualisec"
    strTablesName(6) = "dbo_Section"
    strTablesName(7) = "dbo_Trips"

    For Each td In CurrentDb.TableDefs
       For Each TableName In strTablesName
           If td.Name = TableName Then
              CurrentDb.TableDefs.Delete TableName
           End If
       Next
   Next

   stConnect = "ODBC;Driver={SQL Server};Server=ServerNametest;Database=DBName;Uid=user;Pwd=password;"
   For Each TableName In strTablesName
       Dim splitTarget As Variant
       splitTarget = Split(TableName, "_")
       Set td = CurrentDb.CreateTableDef(TableName, dbAttachSavePWD, splitTarget(1), stConnect)
       CurrentDb.TableDefs.Append td
       AttachDSNLessTable = True
   Next
   Err.Description
End Sub

唯一没有主键的 table 是 SQL 服务器中的 [Personal],我将其链接到 [dbo_personal]

无 DSN 模块 运行 成功并更新了所有链接 table,我真的很挣扎这个应用程序, 请注意,在无 DSN 连接之前,我在构建 DSN

时使用相同的连接字符串

任何有帮助的建议

Access 能够更新 linked tables 只有 当他们有主键时。
如果他们没有,您将收到 "This Recordset is not updateable" 错误。

如果底层 SQL 服务器 table 有主键,Access 通常会检测到它并在 linked table 中使用它。

如果 table 在服务器上 没有 主键,或者如果它是视图而不是 table,则无法访问自己设置主键。

如果您 link 每个 DSN 手动查看,将弹出 window 并要求您 select Access 应视为主键的列:

当您 link table DSN-less(即每个代码)并且您没有指定主键时,Access 将 link table 没有主键。

所以我想你说的 table 要么是视图,要么没有主键。
最初 link 使用 DSN 编辑时,有人 select 在上面显示的 window 弹出窗口中编辑了主键。
当您使用无 DSN 代码重新link编辑它时,您可能没有设置主键,这就是为什么您现在收到 "This Recordset is not updateable" 错误。

解决方案:在使用无 DSN 代码 linking table 后,explicitly set the primary key via VBA code.