-1

I'm trying to copy a record from a form. My code behind the button looks like this:

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

The problem is, when I get to the .Update line, I'm getting an error that says ODBC Call Failed.

If I step through the code, each field appears to resolve correctly, it's just the Update statement that it doesn't seem to like.

Any ideas why this would happen and/or how to correct it?

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117

3 Answers3

0

Not really an answer, but code in comments sucks.

You can get further information about "ODBC Call Failed" with the DBEngine.Errors collection. Run the following code in your error handler:

Dim errX As DAO.Error

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

Edit: when it works, you probably want

Me.Bookmark = .LastModified
Andre
  • 26,751
  • 7
  • 36
  • 80
0

Perhaps you copy the Id?

Here's a proven function to copy a record from a button click:

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
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

For anyone who finds this thread, I was using a similar approach as the poster and was receiving the same error. Turns out, I was violating an index on my sql server table. Make sure you check that and ensure you're not violating any indices, constraints, etc.