You add the CompanyID to the Contacts and Orders tables by setting up the
table relationships so that the Contacts table and the Orders table are in
one-to-many relationship with the Company table, then by assuring the Link
Master and Link Child properties of the subform controls are set to the
linking fields.
tblCompany
CompanyID (Primary key, or PK)
CompanyName
etc.
tblContacts
ContactID (PK)
CompanyID (foreign key, or FK)
FirstName
LastName
etc.
tblOrders
OrderID (PK)
CompanyID (FK)
OrderDate
etc.
Open the Relationships window (Tools > Relationships), and add the three
tables. Drag CompanyID from tblCompany onto CompanyID in tblContacts.
Click Enforce Referential Integrity. Do the same thing with CompanyID in
tblCompany and tblOrders. Note that if CompanyID is autonumber in
tblCompany, it must be Long Integer in tblContacts and in tblOrders;
otherwise the PK and FK fields must be the same data type as established in
tables design view (autonumber is a type of long integer, but it is a
separate selection in table design view). Close the relationship window.
Make a form (frmCompany) based on tblCompany, another (frmOrders) based on
tblOrders, and a third (frmContacts) based on tblContacts. frmOrders and
frmContacts should probably have their Default View set to Continuous to
start.
With frmCompany open in design view, use the toolbox to add a
subform/subreport control. On the Property sheet (click View > Properties
if it is not open), set the Source Object to frmContacts. The Link Child
and Link Master fields should take care of themselves if you have set up the
table relationships correctly. Repeat with another subform control for
frmOrders.
When you add a new record, the FK fields automatically match the PK field.
There is no need to assign the number programatically. Once you start
entering data onto frmCompany you should see places to add Contact and Order
information. To be sure the subforms are showing up on the form, add a
label or something to the headers for frmOrders and frmContacts so that even
if there are no records you can see the subform is there.