copy field from main form to sub form

S

Song Su

I have main form frmPO with subform frmPOdetailsSubform. When create new
record in main form, I want PODescription field (after update event?) in
main to automatically copy to ItemDescription field in subform. How to do
that?
 
J

John W. Vinson

I have main form frmPO with subform frmPOdetailsSubform. When create new
record in main form, I want PODescription field (after update event?) in
main to automatically copy to ItemDescription field in subform. How to do
that?

Why would you WANT to do it!?

You can *display* the description in the subform (repeatedly, all the same for
all rows) by setting the control source of a textbox to

=Parent!ItemDescription

If you're trying to *store* the PO Description redundantly in both the parent
form's table and the child table... don't. Or at least explain why.
 
S

Song Su

Each PO has PO Description and each PO has one or more items. For entering
new PO, I assume this PO has only one item and that's why I want to copy PO
Description to ItemDescription (I don't want any PO without item in
subform.) Of cause users can add more items with different item description
in the subform
 
J

John W. Vinson

Each PO has PO Description and each PO has one or more items. For entering
new PO, I assume this PO has only one item and that's why I want to copy PO
Description to ItemDescription (I don't want any PO without item in
subform.) Of cause users can add more items with different item description
in the subform

Would it be OK for every item on the subform to inherit the *same*
description?

If so you can set the DefaultValue property of the subform's ItemDescription
field to

=Forms!mainformname!PODescription
 
S

Song Su

=Forms!mainformname!PODescription
would show in every PO's subform as New (the last line) and I don't want
users to panic or I have to explain it. I think COPY podescription to
itemdescription is much cleaner and it's just for the NEW po's first record
in item description in subform
 
J

John W. Vinson

=Forms!mainformname!PODescription
would show in every PO's subform as New (the last line) and I don't want
users to panic or I have to explain it. I think COPY podescription to
itemdescription is much cleaner and it's just for the NEW po's first record
in item description in subform

Then you may need to "push" it in using the subform's BeforeInsert event:

Private Sub Form_BeforeInsert(Cancel as Integer)
' Check to see if you're on the first subform record
If Me.RecordsetClone.RecordCount <= 1 Then
Me!ItemDescription = Parent!PODescription
End If
End Sub

Tweak as needed, untested code.
 
S

Song Su

I use your code in BeforeInsert event of the subform. Compiled without
error. However, when I add a new record in PO, type PODesription, save
record, no record is inserted into subform. I opened PO table, record
inserted. But PODetails table does not have record inserted.
 
J

John W. Vinson

I use your code in BeforeInsert event of the subform. Compiled without
error. However, when I add a new record in PO, type PODesription, save
record, no record is inserted into subform. I opened PO table, record
inserted. But PODetails table does not have record inserted.

The beforeinsert event will not do anything until you explicitly "dirty" the
record by manually entering something in some other field of the subform.

What would be the point of inserting a record in the subform's table
containing NOTHING other than a single record containing only information
which already exists in the parent table???
 
S

Song Su

Got it. I have to input something in other field in subform so PODescrition
can copied to ItemDescription. Work exactly as planned. Thank you very much.
 

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