Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble adding new records in child form

Status
Not open for further replies.

megaman3

Programmer
Aug 11, 2003
2
CA
Hi, I have two tables, one called Deliverables and the other called TestScenarios. They are linked in a
one-to-many relationship, with Deliverables being on the "one" side of the relationship. I also have a form corresponding to each table, called frmDeliverables and frmTestScenarios recpectively.

Because frmDeliverables is a subform of another form, and there is limited screen real estate, I created a button on frmDeliverables that pops up frmTestScenarios such that the latter is filtered to display only the records in the TestScenarios table that are related to the current record shown in frmDeliverables.

This works fine until I try to add a new record in the frmTestScenarios form. At this point, all the fields, including the foreign key field which links the record in frmTestScenarios to the one in frmDeliverables, are set to nothing. Ideally I would like the foreign key field to be hidden. Is there a way to set the value of the foreign key field in frmTestScenarios to the primary key of the record shown in frmDeliverables, whenever a new record is created in frmTestScenarios?

Thanks a lot!
 
If I understand you correctly, Yes, there are a few ways
but being a programmer they are all in Code depending upon how I open the form.

*********** If you use doCmd

a) In your button to pop up the frmTestScenarios add an OpenArgs (I think that is the name - I'm not at my Access PC so can't check) parameter to the doCmd which opens the form, and set this to hold the foreign key value.

b) In the frmTestScenarios form add a variable

eg Dim mMyForeignKey as whatever

c) Then in the form_load for frmTestScenarios test for the OpenArgs, if present read out the foreign key into the form level variable and everytime you do 'add new', you can set the foreign key from the form variable.


************ IF you don't use doCmd

but open the form via a form variable in code, the process would be slightly different, in that you would need to treat the frmTestScenarios form as a Class, and add a LET procedure for the myForeignKey variable, then set this to the value of your foreign key after you have instantiated the form.


c) Finally and possible simplest, assuming the main/parent form is always open - use the value from the parent form directly - similar to this -

[Forms]![myParentForm]!myForeignKeyField



 
How do you add a new record? It is cleaner if you add to the underlying table and do a requery.

rollie @bwsys.net
 
Thanks a lot TelcoBT! That was a very clear, understandable answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top