【问题标题】:RecordsetClone is causing an ODBC errorRecordsetClone 导致 ODBC 错误
【发布时间】:2016-07-24 02:58:48
【问题描述】:

我正在尝试从表单中复制记录。我的按钮后面的代码如下所示:

With Me.RecordsetClone
   .AddNew
      !TableField1 = Me.CorrespondingTextboxName1
      !TableField2 = Me.CorrespondingTextboxName2
      … etc for the rest of the fields                 
  .Update
  .Bookmark = .LastModified
End With

问题是,当我到达.Update 行时,我收到一条错误消息,上面写着ODBC Call Failed

如果我单步执行代码,每个字段似乎都能正确解析,这只是它似乎不喜欢的 Update 语句。

任何想法为什么会发生这种情况和/或如何纠正它?

【问题讨论】:

    标签: vba ms-access-2010 recordset


    【解决方案1】:

    不是真正的答案,但 cmets 中的代码很烂。

    您可以通过 DBEngine.Errors 集合获得有关“ODBC 调用失败”的更多信息。在错误处理程序中运行以下代码:

    Dim errX As DAO.Error
    
    For Each errX In Errors
        Debug.Print errX.Number & ": " & errX.Description
    Next errX
    

    编辑:当它起作用时,你可能想要

    Me.Bookmark = .LastModified
    

    【讨论】:

      【解决方案2】:

      也许你复制了 Id?

      这是一个经过验证的功能,可以通过单击按钮复制记录:

      Private Sub btnCopy_Click()
      
        Dim rstSource   As DAO.Recordset
        Dim rstInsert   As DAO.Recordset
        Dim fld         As DAO.Field
      
        If Me.NewRecord = True Then Exit Sub
      
        Set rstInsert = Me.RecordsetClone
        Set rstSource = rstInsert.Clone
        With rstSource
          If .RecordCount > 0 Then
            ' Go to the current record.
            .Bookmark = Me.Bookmark
            With rstInsert
              .AddNew
                For Each fld In rstSource.Fields
                  With fld
                    If .Attributes And dbAutoIncrField Then
                      ' Skip Autonumber or GUID field.
                    Else
                      ' Copy field content.
                      rstInsert.Fields(.Name).Value = .Value
                    End If
                  End With
                Next
              .Update
              ' Go to the new record and sync form.
              .MoveLast
              Me.Bookmark = .Bookmark
              .Close
            End With
          End If
          .Close
        End With
      
        Set rstInsert = Nothing
        Set rstSource = Nothing
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        对于找到此线程的任何人,我使用的方法与发帖人类似,并且收到了相同的错误。原来,我违反了我的 sql server 表上的索引。确保您检查并确保您没有违反任何索引、约束等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-18
          • 1970-01-01
          • 2012-05-20
          • 1970-01-01
          • 2012-07-09
          • 2016-08-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多