Question Locked

Copy a field's data from an Access database record into another record

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have an access database with a generated key in the format DAA00001X ( not using autonumbering in the main table).

What I want to be able to do is to copy a record from the table to a new record. I cannot seem to use the PasteAppend code for some reason - perhaps it is because of the generated key rather than using the autonumbering.

Is there some way to copy individual data field values across to the new record.

I have some code that finds the record and saves the name to a field called Firstfield. What is held in this field is the correct data but I cannot seem to get it to go into the new record. When I attempted to Update it updated all name fields in the table with the firstfield name value rather than just the current record.

Is someone able to help?

I have attached my sample code:
_____________________________________________
Private Sub cmdCopyRecord_Click()

On Error GoTo Err_cmdCopyRecord_Click

Dim rst As Recordset
Dim fld As Field
Dim StLinkCriteria As String
Dim stDocName As String
Dim strSQL As String

Set rst = Me.RecordsetClone
'opens the form to assign the delegation no
stDocName = "frmNewDel"
DoCmd.OpenForm stDocName, , , StLinkCriteria
With rst
.MoveLast
Firstfield = Me![name]
Me![name] = .Fields("name")
Debug.Print Firstfield
End With

'the below updates all name fields with the one just chosen - NOT
'what I want - only want to update the current record with the
'Firstfield data

strSQL = "UPDATE [Delegation] SET name = '" & Firstfield & "'"
CurrentDb.Execute strSQL

DoCmd.Close acForm, "frmDelProperties"

Exit_cmdCopyRecord_Click:
Exit Sub

Err_cmdCopyRecord_Click:
MsgBox Err.Description
Resume Exit_cmdCopyRecord_Click

End Sub
___________________________________________

Thanks for any help.

3 replies

by andre

Hi
I will suggest to call a query first for the field you want to change.

select * from (your table name) where delegation ='" & (your field for lookup) & "'"

with (your recordset)

![(your field)]=(your value)

end with

(your recodset).update

this is for vb working with sql.
You should be able to use the same example from access.

Then update it

by tonialbrown

Thanks for the reply Andre, I had some problems activating my account and then was away and have only received the response just now.

I am still not able to get this working.

My problem is I call the Form frmNewDel that opens the drop down boxes to pick my assigned new key (id)

However this does not execute until after the code in this form is executed which means I am unable to capture the record set rs2 for the new record to copy the values to. This means I can not run the SQL code as suggested to find the record where the values are to be copied into.

Are you able to see what I am doing wrong?? Thanks in advance.

Sample code:

Private Sub cmdCopyRecord_Click()

On Error GoTo Err_cmdCopyRecord_Click

Dim rst As Recordset
Dim rs2 As Recordset

Dim fld As Field
Dim StLinkCriteria As String
Dim stDocName As String
Dim strSQL As String

Set dbs = CurrentDb()
Set rst = Me.RecordsetClone

'opens the form to assign to
'the delegation number
'THIS IS NOT EXECUTED UNTIL AFTER THE OTHER COMMANDS - NOT SURE WHY

stDocName = "frmNewDel"
DoCmd.OpenForm stDocName, , , StLinkCriteria

Set rs2 = db.OpenRecordset("select * from delegation where delegation.id = """ & [id] & """;")

With rs2
.Edit
![name] = Firstfield
End With
rs2.update
Exit_cmdCopyRecord_Click:
Exit Sub

Err_cmdCopyRecord_Click:
MsgBox Err.Description
Resume Exit_cmdCopyRecord_Click

End Sub

I have attached the latest changes to code, to see if you are able to see what is wrong.

This discussion is closed to new replies.