2016-04-04 66 views
0

我试图复制表单中的记录。我后面的按钮的代码如下所示:RecordsetClone导致ODBC错误

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语句。

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

回答

0

不是一个真正的答案,但评论中的代码很糟糕。

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

Dim errX As DAO.Error 

For Each errX In Errors 
    Debug.Print errX.Number & ": " & errX.Description 
Next errX 

编辑:它工作时,你可能想

Me.Bookmark = .LastModified 
0

也许你拷贝的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