best way to copy a record?

J

Jerome

Hi,

(using MS Access 2000)

I've got a form (containing subforms) displaying data related to the
record. Now I want to copy the data from the main form (not the
subforms) into a new record (in the same table) and then move to that
record to edit the copy.

What's the best way to do that? I imagine doing it by code? But could
somebody please give me an example. I tried looking on the internet, but
haven't found anything really helpful.

Any help is greatly appreciated!

Jerome
 
G

Geof Wyght

Jerome,
While on your current, old record, store the data in
variables:
strName = Me.Name.Value and so on.
Then do:
DoCmd.GoToRecord , , acNewRec
When you at your new record do:
Me.Name.Value = strName and so on.
Geof.
 
M

Marshall Barton

Jerome said:
(using MS Access 2000)

I've got a form (containing subforms) displaying data related to the
record. Now I want to copy the data from the main form (not the
subforms) into a new record (in the same table) and then move to that
record to edit the copy.

What's the best way to do that? I imagine doing it by code? But could
somebody please give me an example. I tried looking on the internet, but
haven't found anything really helpful.


Best way is very subjective. The easiest, but very crude,
way is to use the command button wizard to duplicate the
current record.

A way that gives you a high degree of control is to copy the
fields from the current record to a new record in the form's
RecordsetClone:

With Me.RecordsetClone
.AddNew
!fieldA = Me.fieldA
!fieldC = Me.fieldC
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

hi readers of the thread....

i've not been aware that there's a wizard command that lets you create a
'duplicate record' button. that's what i'm trying to do on my nested sub-form
in my a2k mdb.

using the wizard, i see that the following code appeared on the 'ON Click'
event of my sub-form's latest 'Duplicate Record' command button.

it seems to work like a charm at first blush, generating one more record in
the total based upon looking at the record navigator at the bottom of the
nested sub-forms's; initially record 1 of 8 which becomes 1 of 9.

one thing seems to be awry though, when i try to use the mouse wheel and/or
save the record, iget the following from a2k:

"The value you entered isn't appropriate for the input mask: '00/00/00'
specified for this field".

which i presume has something to do with the values of the date field
textboxes (which i'm using "Medium Date" format to display).

can anyone help decode/solve this problem.

before i forget, here's the actual 'Duplicate Record' cmd button code
courtesy of the 'wizard's:

Private Sub Command47_Click()
On Error GoTo Err_Command47_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

Exit_Command47_Click:
Exit Sub

Err_Command47_Click:
MsgBox Err.description
Resume Exit_Command47_Click

End Sub


for whatever it's worth.

thanks a bunch in advance of your bandwidth.

ted
 
M

Marshall Barton

Ted said:
i've not been aware that there's a wizard command that lets you create a
'duplicate record' button. that's what i'm trying to do on my nested sub-form
in my a2k mdb.

using the wizard, i see that the following code appeared on the 'ON Click'
event of my sub-form's latest 'Duplicate Record' command button.

it seems to work like a charm at first blush, generating one more record in
the total based upon looking at the record navigator at the bottom of the
nested sub-forms's; initially record 1 of 8 which becomes 1 of 9.

one thing seems to be awry though, when i try to use the mouse wheel and/or
save the record, iget the following from a2k:

"The value you entered isn't appropriate for the input mask: '00/00/00'
specified for this field".

which i presume has something to do with the values of the date field
textboxes (which i'm using "Medium Date" format to display).

can anyone help decode/solve this problem.

before i forget, here's the actual 'Duplicate Record' cmd button code
courtesy of the 'wizard's:

Private Sub Command47_Click()
On Error GoTo Err_Command47_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append [snip]
for whatever it's worth.


Well, it's not really worth all that much ;-)

If you decode those stupid MenuItem numbers, you'll see that
it simply simulates a user performing:
SelectRecord
Copy
GoToNewRecord
Paste

Part of what that implies is that it can only copy visible
data on the form and when it pastes it back into a new
record, it invokes the usual activities associated with user
actions (e.g. input mask checking).

A far more reliable and flexible approach is to use code to
copy the real data in the form's record one field at a time
skipping all the UI related stuff. You also have the
flexibility of not copying any inappropriate fields (e.g.
the PK field) and/or setting values that are not visible on
the form (e.g. a date completed field).

Here's an outline of the kind of code I use:

With Me.RecordsetClone
.AddNew
!fieldA = Me.fieldA
!fieldB = Me.fieldB
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

hi,

i've got a question about the code below (vba newbie that i am),

With Me.RecordsetClone
.AddNew
!fieldA = Me.fieldA
!fieldC = Me.fieldC
. . .
.Update
Me.Bookmark = .LastModified
End With

would you mind fleshing it out so to speak just a bit. i have this sub-form
i've put a "Duplicate" button onto the header of so as to enable the user to
duplicate the active record of the sub-form's. [note though: i'm getting a
"The value you entered isn't appropriate for the input mask '00/00/00'
specified for the field" type message".]. also, just for grins, suppose you
wanted to alter the value of one of the fields being duplicated onto the next
record from "Yes" to "No"?

as usual, thanks for any bandwidth!

ted
 
G

Guest

marsh,

i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):
With Me.RecordsetClone
.AddNew
!fieldA = Me.fieldA
!fieldB = Me.fieldB
. . .
.Update
Me.Bookmark = .LastModified
End With

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

ted

Marshall Barton said:
Ted said:
i've not been aware that there's a wizard command that lets you create a
'duplicate record' button. that's what i'm trying to do on my nested sub-form
in my a2k mdb.

using the wizard, i see that the following code appeared on the 'ON Click'
event of my sub-form's latest 'Duplicate Record' command button.

it seems to work like a charm at first blush, generating one more record in
the total based upon looking at the record navigator at the bottom of the
nested sub-forms's; initially record 1 of 8 which becomes 1 of 9.

one thing seems to be awry though, when i try to use the mouse wheel and/or
save the record, iget the following from a2k:

"The value you entered isn't appropriate for the input mask: '00/00/00'
specified for this field".

which i presume has something to do with the values of the date field
textboxes (which i'm using "Medium Date" format to display).

can anyone help decode/solve this problem.

before i forget, here's the actual 'Duplicate Record' cmd button code
courtesy of the 'wizard's:

Private Sub Command47_Click()
On Error GoTo Err_Command47_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append [snip]
for whatever it's worth.


Well, it's not really worth all that much ;-)

If you decode those stupid MenuItem numbers, you'll see that
it simply simulates a user performing:
SelectRecord
Copy
GoToNewRecord
Paste

Part of what that implies is that it can only copy visible
data on the form and when it pastes it back into a new
record, it invokes the usual activities associated with user
actions (e.g. input mask checking).

A far more reliable and flexible approach is to use code to
copy the real data in the form's record one field at a time
skipping all the UI related stuff. You also have the
flexibility of not copying any inappropriate fields (e.g.
the PK field) and/or setting values that are not visible on
the form (e.g. a date completed field).

Here's an outline of the kind of code I use:

With Me.RecordsetClone
.AddNew
!fieldA = Me.fieldA
!fieldB = Me.fieldB
. . .
.Update
Me.Bookmark = .LastModified
End With
 
M

Marshall Barton

Ted said:
marsh,

i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):


i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.

sounds like more's needed doesn't it.




Marshall Barton said:
Ted said:
marsh,

i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):


i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
M

Marshall Barton

That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.
--
Marsh
MVP [MS Access]


i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.

Marshall Barton said:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

hi marshall,

in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



Marshall Barton said:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.
--
Marsh
MVP [MS Access]


i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.

Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?
Marshall Barton said:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
M

Marshall Barton

I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.
--
Marsh
MVP [MS Access]


in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



Marshall Barton said:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.

i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

another one yours truly's achilees heel's march....

ted



Marshall Barton said:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.
--
Marsh
MVP [MS Access]


in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



Marshall Barton said:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

breakpoints... how does that work marsh?

Marshall Barton said:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.
--
Marsh
MVP [MS Access]


in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



Marshall Barton said:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
M

Marshall Barton

When you're viewing the code that you're working on, click
in the far left margin of a line where you want execution to
stop, The line should then be highlighted (usually in dark
red).

Then back to your form in design view, switch the form to
form view and click on whatever starts the record dup
process. When the code stops at the break point, hover the
mouse over a term in a line to get a little popup box with
the value of the item. If that isn't sufficient, hit ctrl+g
to open the immediate/debug window and type something useful
such as:
? Me!somefield
to display the value of somefield. In general, you're just
poking around to see what clues you can pick up.

There are a ton of debugging features in the VB environment
so check Help for details.

One point of caution. Do not edit your code while your form
is still open in form view. Always switch the form back to
design view before making any changes to the code. After
you make changes, be sure to compile (Debug menu) before
switching the form back to form view.
--
Marsh
MVP [MS Access]


breakpoints... how does that work marsh?

Marshall Barton said:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.


in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

hi marsh, i guess the response i thought i wrote you (or perhaps just
started to write you) never happened today. it's been kindof above average
hectic and i've not had a chance to work the vba editor just yet --

this is really good info, mini-tutorial, that they probably just sort of
loopily covered in the 3day wonder intro to ms vba access programming i took
about a year ago and for whatever reason, i don't get 'round to having to
delve into.

what i learned about my users' needs regarding all this record duplication
effort is that when they want to duplicate it, they want it to belong to the
next value of the 'cycle' control's (which is one of the constituents of the
5 control PK), ie. to have the values belong on a record where 'cycle' =
'cycle' + 1. i'd imagine that your code would read something like "!Cycle =
Me.Cycle + 1" at the appropriate point. would i be wrong about this?

on a slightly different tack, on 10/11 before you entered this thread, Geof
Wyght opined that

"Jerome,
While on your current, old record, store the data in
variables:
strName = Me.Name.Value and so on.
Then do:
DoCmd.GoToRecord , , acNewRec
When you at your new record do:
Me.Name.Value = strName and so on.
Geof."

in absolute terms, would that work.

and third and lastly, your code.....is it intended to go into the cmdbtn's
'On Click' event as written, w/o anything else...i'm wondering how it 'knows'
to align itself w/ the PK values of the main form's? does it do that
automatically?

best,

ted




Marshall Barton said:
When you're viewing the code that you're working on, click
in the far left margin of a line where you want execution to
stop, The line should then be highlighted (usually in dark
red).

Then back to your form in design view, switch the form to
form view and click on whatever starts the record dup
process. When the code stops at the break point, hover the
mouse over a term in a line to get a little popup box with
the value of the item. If that isn't sufficient, hit ctrl+g
to open the immediate/debug window and type something useful
such as:
? Me!somefield
to display the value of somefield. In general, you're just
poking around to see what clues you can pick up.

There are a ton of debugging features in the VB environment
so check Help for details.

One point of caution. Do not edit your code while your form
is still open in form view. Always switch the form back to
design view before making any changes to the code. After
you make changes, be sure to compile (Debug menu) before
switching the form back to form view.
--
Marsh
MVP [MS Access]


breakpoints... how does that work marsh?

Marshall Barton said:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.



Ted wrote:
in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

i tried replying to this but got an operations error from the system....

it's been a-bove a-verage hectic today so still haven't had time to get a
fix on this great mini-tutorial you've been generous enough to provide. will
try tomorrow, if they let me alone.

the user's are telling me now that when they want to duplicate a record in
the sub-form, that the duplicate record will have value of one of the members
of the sub-form's PK (as well as the main-form's PKs) of Cycle + 1. in
another words, the duplicated record for a cycle 1 record will have a value
of cycle of 2, if the duplication happens on a cycle value of 3 record, then
the duplicated record's cycle value will be 4, and so forth. i didn't know
that at the time i initiated this, but i imagine that using your code, it'd
go something like "!Cycle = Me.Cycl1 + 1". am i wrong?

next up, if all of this debug effort is for naught, can i use the practice
of Geof Wyght's that he submitted just prior to your joining this thread
around 10/11?

lastly, using your code, how does the system know to align the pks of the
sub-form's with the mainform record's values?

best,

ted
Marshall Barton said:
When you're viewing the code that you're working on, click
in the far left margin of a line where you want execution to
stop, The line should then be highlighted (usually in dark
red).

Then back to your form in design view, switch the form to
form view and click on whatever starts the record dup
process. When the code stops at the break point, hover the
mouse over a term in a line to get a little popup box with
the value of the item. If that isn't sufficient, hit ctrl+g
to open the immediate/debug window and type something useful
such as:
? Me!somefield
to display the value of somefield. In general, you're just
poking around to see what clues you can pick up.

There are a ton of debugging features in the VB environment
so check Help for details.

One point of caution. Do not edit your code while your form
is still open in form view. Always switch the form back to
design view before making any changes to the code. After
you make changes, be sure to compile (Debug menu) before
switching the form back to form view.
--
Marsh
MVP [MS Access]


breakpoints... how does that work marsh?

Marshall Barton said:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.



Ted wrote:
in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.



:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
M

Marshall Barton

Ahhh... I missed the part about the new record being
associated with the "next" cycle. I'll bet the missing
parent record is the next cycle record. You'll have to make
sure that exists before "dup"ing the child record. I could
have sworn that you wnated to dup the main form record, but
now it sounds like you're working on a subform record so I'm
kind of confused here. Maybe you should try explaining this
"dup" process again so I can get a better grip on what we're
trying to do here.

If you were duping the main form record, then, yes, the code
would be in the dup button's click event procedure. And,
yes, that is how you would assign the next cycle value. If
the button is in the main form and you want to dup both the
main form record and a subform record, then there's more
work to do, but I don't understand enough yet to describe
the code.

Sure Geof's code will work, but it does essentially the same
thing as my code except that you must declare all those
variables, save all the values to to the variables, then get
to the new record and copy all the variables back to the new
record's controls. Using the RecordsetClone allows you to
copy the form's control values directly to a new record
using only one third the number of lines of code.
--
Marsh
MVP [MS Access]


this is really good info, mini-tutorial, that they probably just sort of
loopily covered in the 3day wonder intro to ms vba access programming i took
about a year ago and for whatever reason, i don't get 'round to having to
delve into.

what i learned about my users' needs regarding all this record duplication
effort is that when they want to duplicate it, they want it to belong to the
next value of the 'cycle' control's (which is one of the constituents of the
5 control PK), ie. to have the values belong on a record where 'cycle' =
'cycle' + 1. i'd imagine that your code would read something like "!Cycle =
Me.Cycle + 1" at the appropriate point. would i be wrong about this?

on a slightly different tack, on 10/11 before you entered this thread, Geof
Wyght opined that

"Jerome,
While on your current, old record, store the data in
variables:
strName = Me.Name.Value and so on.
Then do:
DoCmd.GoToRecord , , acNewRec
When you at your new record do:
Me.Name.Value = strName and so on.
Geof."

in absolute terms, would that work.

and third and lastly, your code.....is it intended to go into the cmdbtn's
'On Click' event as written, w/o anything else...i'm wondering how it 'knows'
to align itself w/ the PK values of the main form's? does it do that
automatically?


Marshall Barton said:
When you're viewing the code that you're working on, click
in the far left margin of a line where you want execution to
stop, The line should then be highlighted (usually in dark
red).

Then back to your form in design view, switch the form to
form view and click on whatever starts the record dup
process. When the code stops at the break point, hover the
mouse over a term in a line to get a little popup box with
the value of the item. If that isn't sufficient, hit ctrl+g
to open the immediate/debug window and type something useful
such as:
? Me!somefield
to display the value of somefield. In general, you're just
poking around to see what clues you can pick up.

There are a ton of debugging features in the VB environment
so check Help for details.

One point of caution. Do not edit your code while your form
is still open in form view. Always switch the form back to
design view before making any changes to the code. After
you make changes, be sure to compile (Debug menu) before
switching the form back to form view.

breakpoints... how does that work marsh?

:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.



Ted wrote:
in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.


:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 
G

Guest

the part about the 'next' cycle just came on the radar for me today, so you
didn't 'miss' anything really; it's a new 'need' of theirs. i have always
been conceptualizing this thing as involving a duplicate of a sub-form record
(for which presumably the parent record will always exist) but as my users
seem to speak a language all their own at times, things can and will 'get
lost in translation'. so the aim is apparently for the data entry clerk to be
able to 'tag' a sub-form record as worthy of appearing in the subsequent
cycle. [btw-- this has all to do with side-effects of experimental drug
treatments and their continuing from one to another 'cycle' in the life of a
clinical trial and the obligation to record what they are]

at this point in my inexpert level of sophistication, if a listbox field
called "Continuing" (meaning continuing into the next cycle) is checked
"Yes", we will want to utilize a "Duplicate" record button which has had its
properties set to "enabled" and appears in the sub-form's header to duplicate
(as far as i can gather) the values of all the fields in the sub-form into a
record that appears as a record in the sub-form for the same combination of
the 5 member PK with the exception that one of them, i.e. "Cycle" will be
equal to "Cycle + 1" as i think i wrote earlier. i don't think that there's
anything in the main form that requires being duped unless i've misunderstood
them --- they're just talking about avoiding the hassle of having to
re-record stuff from a sub-form record from one cycle to the next the long
way and to get out of doing data entry that could be done by the smarts we
build into the application.

i hope this is making sense. if not i'll spin it again.

ted



Marshall Barton said:
Ahhh... I missed the part about the new record being
associated with the "next" cycle. I'll bet the missing
parent record is the next cycle record. You'll have to make
sure that exists before "dup"ing the child record. I could
have sworn that you wnated to dup the main form record, but
now it sounds like you're working on a subform record so I'm
kind of confused here. Maybe you should try explaining this
"dup" process again so I can get a better grip on what we're
trying to do here.

If you were duping the main form record, then, yes, the code
would be in the dup button's click event procedure. And,
yes, that is how you would assign the next cycle value. If
the button is in the main form and you want to dup both the
main form record and a subform record, then there's more
work to do, but I don't understand enough yet to describe
the code.

Sure Geof's code will work, but it does essentially the same
thing as my code except that you must declare all those
variables, save all the values to to the variables, then get
to the new record and copy all the variables back to the new
record's controls. Using the RecordsetClone allows you to
copy the form's control values directly to a new record
using only one third the number of lines of code.
--
Marsh
MVP [MS Access]


this is really good info, mini-tutorial, that they probably just sort of
loopily covered in the 3day wonder intro to ms vba access programming i took
about a year ago and for whatever reason, i don't get 'round to having to
delve into.

what i learned about my users' needs regarding all this record duplication
effort is that when they want to duplicate it, they want it to belong to the
next value of the 'cycle' control's (which is one of the constituents of the
5 control PK), ie. to have the values belong on a record where 'cycle' =
'cycle' + 1. i'd imagine that your code would read something like "!Cycle =
Me.Cycle + 1" at the appropriate point. would i be wrong about this?

on a slightly different tack, on 10/11 before you entered this thread, Geof
Wyght opined that

"Jerome,
While on your current, old record, store the data in
variables:
strName = Me.Name.Value and so on.
Then do:
DoCmd.GoToRecord , , acNewRec
When you at your new record do:
Me.Name.Value = strName and so on.
Geof."

in absolute terms, would that work.

and third and lastly, your code.....is it intended to go into the cmdbtn's
'On Click' event as written, w/o anything else...i'm wondering how it 'knows'
to align itself w/ the PK values of the main form's? does it do that
automatically?


Marshall Barton said:
When you're viewing the code that you're working on, click
in the far left margin of a line where you want execution to
stop, The line should then be highlighted (usually in dark
red).

Then back to your form in design view, switch the form to
form view and click on whatever starts the record dup
process. When the code stops at the break point, hover the
mouse over a term in a line to get a little popup box with
the value of the item. If that isn't sufficient, hit ctrl+g
to open the immediate/debug window and type something useful
such as:
? Me!somefield
to display the value of somefield. In general, you're just
poking around to see what clues you can pick up.

There are a ton of debugging features in the VB environment
so check Help for details.

One point of caution. Do not edit your code while your form
is still open in form view. Always switch the form back to
design view before making any changes to the code. After
you make changes, be sure to compile (Debug menu) before
switching the form back to form view.


Ted wrote:
breakpoints... how does that work marsh?

:
I don't know what else to say Ted. The error message says
what it says.

I guess I would start setting some breakpoints and double
check the values of every field before the Update executes.



Ted wrote:
in looking at the relationships between the parent (treatment and toxicity)
and child (Adverse Events (child)) tables and in trying to grasp the meaning
behind your diagnosis, i see that the parent's pk's consist of just "patient
number" and "current cycle number" and these are in a one-to-many
relationship with child's "patient number" and "cycle" (which are 2 of the 5
members of the composite pk of child's -- the remaining once again being
"category", "subtype", "toxicity", and lastly "onset".

what's troubling me by your diagnosis is that as i understood it the
duplication is occuring on a sub-record that has no counterpart in the parent
table, which i believe could not be the case inasmuch as i actuated the
duplication on a form that clearly had values of the child's in the parent'
(main form) part.


:
That message means that the new record has a foreign key
that doesn't match up with the primary key of any record in
its related parent table (probably the main form's record
source table).

I would guess that either you forgot to copy that field or
you set it to some inappropriate value.


Ted wrote:
i've been doing some experimenting on the hunch that the code you generated
below would look like yours and i'm getting a "Run time error 3201 You cannot
add or change a record because a related record is required in table
"Treatment and Toxicity" " and my code window has ".Update" highlighted in
yellow.


Ted wrote:
i did some snooping around the help documentation and sort of got the
impression that that was what the wizardly software was up to.

finding myself in the vba newbie dimension, i'm not clear what all that code
means and where to plonk it (in the "click event" of my "Duplicate Record"
cmdbtn i suppose):

i mean, assuming that we're talking about my sub-form and that i have
patient_number (#), cycle (#), ae_description (txt), subtype (txt), onset
(date/time) comprising the composite PK and a litany of sundry other fields
like resolved (date/time), grade (#),....., continuing (txt) in the table
behind the sub-form and the user wants to 'clone' the active record when they
click "Duplicate Record" w/ the exception that one field's values get
substituted for another's (e.g. resolved's get put into onsets's, resolved's
are set to empty, and continuing's becomes "No" from "Yes") what does my code
begin to look like at this point?

:
Yes it goes in the button's click event and it will look
like the template code above with your own field names:

With Me.RecordsetClone
.AddNew
![patient_number] = Me.[patient_number]
!cycle = Me.cycle
. . .
!resolved = Null
!onset = Me.resolved
!continuing = "No"
. . .
.Update
Me.Bookmark = .LastModified
End With
 

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