Subform on an unbound main form

G

Guest

I have a subform on an unbound main form (there are many reasons the form is
unbound). The subform is created from a linking table containing two ID
fields which allows a "many to many" relationship to be two "one to manys."
When I enter data on the main form, the subform automatically generates an
incomplete record (it is missing the ID field from the main form because the
main form doesn't create the autonumber ID field until it is saved with a
command button). I tried making the linking table's fields, both be primary
keys, but then when activating the main form, I get a message "Field can not
have a null value". If I take off the primary keys, then it writes a record
(an empty record) to the linking table everytime I run the main form.
How do I have the subform wait for the autonumber ID field on the main form
and then create a complete record in the subform (which is bound to the
linking table)?
 
A

Albert D. Kallal

If you main form is not bound, then you will have to write some custom code
in the sub-form to "set" the foreign key value.

when you have a bound form, ms-access automatically FORCES the main form to
write to disk, and thus forces the creating of the primary key. Thus, when
you edit child records, it ensures that the one to many relationship can be
enforced, and a key value is available. It kind of makes sense, as how can
you add child records without first knowing, and having a key value in the
parent table.

So, since the form is un-bound, then just dump the use of the link
master/child (they only work with bound forms). If you go the un-bound
route..then you just have to code this stuff your self. I would place the
code in the sub-forms on-insert event.

Just go:

me!MyFieldUsedToLinkToParentKeyValue = me.Parent.TheKeyValue
 

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