2016-04-04 139 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