How to copy some fields in current record into new record?

C

Chris

I have access 2000 and I wish to be able to copy 5 out of
about 10 fields into a new record automatically using a
command button on a form. Similar to the way the duplicate
buttom works but only with these selected fields. None of
the fields I wish to duplicate are the primary key.

Thank muchly

Chris
 
L

Larry Linson

In the form's module, Dim a variable for each Control whose contents you
want carried forward, in the Click event of the Command Button copy the
Controls' values to the variable. Then, in the OnCurrent event of the new
Record, copy the content of the variables to the appropriate Controls on the
new Record.

Larry Linson
Microsoft Access MVP
 
A

Armen Stein

In the form's module, Dim a variable for each Control whose contents you
want carried forward, in the Click event of the Command Button copy the
Controls' values to the variable. Then, in the OnCurrent event of the new
Record, copy the content of the variables to the appropriate Controls on the
new Record.

Larry Linson
Microsoft Access MVP

Be careful here - the OnCurrent event will fire on existing records too,
thereby copying the saved data into them also.

You can check the Me.NewRecord property and only copy the fields if it
is True.
 
D

Dirk Goldgar

Armen Stein said:
Be careful here - the OnCurrent event will fire on existing records
too, thereby copying the saved data into them also.

You can check the Me.NewRecord property and only copy the fields if it
is True.

I think if the idea is to click the command button and thereby start a
new record with certain fields already filled in from the current
record, I wouldn't use the Current event for anything. The command
button's Click event would do it all, something like this:

'----- start of example code -----
Private Sub cmdCopyRecord_Click()

Dim v1 As Variant
Dim v2 As Variant
Dim v3 As Variant
Dim v4 As Variant
Dim v5 As Variant

v1 = Me!Field1.Value
v2 = Me!Field2.Value
v3 = Me!Field3.Value
v4 = Me!Field4.Value
v5 = Me!Field5.Value

RunCommand acCmdRecordsGoToNew

Me!Field1 = v1
Me!Field2 = v2
Me!Field3 = v3
Me!Field4 = v4
Me!Field5 = v5

End Sub
'----- end of example code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top