relating more than one
RH

Forum home » Delegate support and help forum » Microsoft Access Training and help » Relating more than one foreign key

Relating more than one foreign key

resolvedResolved · High Priority · Version 2002/XP

Joe has attended:
Access Intermediate course

Relating more than one foreign key

Dear BestSTL,

There's a lack of understanding on my part as to how tables should be related when relating more than one foreign key.
(Please see screen shots attached to a separate email sent with the same title)

Table set up (RelationshipWindow.gif)
I have an orders table (TBL_STORES_Orders) with 3 fields to open the possibility of having 3 different contacts (Confirmation, Delivery & Invoice) assigned to one order.
TBL_STORES_Orders has a look-up for these 3 fields to the contacts table (TBL_STORES_Contacts)
When TBL_STORES_Orders is added to the relationship table, right-click > 'Show Direct' is selected, 3 instances of TBL_STORES_Contacts appear, each with one relationship line to one of the TBL_STORES_Orders fields.

Query set up (QueryDesign.gif)
I now need these tables to be included in a query so I can create a report (Confirmation, Delivery or Invoice)
After attaching TBL_STORES_Orders then TBL_STORES_Contacts, 3 relationship lines display; one each from TBL_STORES_Orders fields all joining up to TBL_STORES_Contacts primary key (ID_Contact).
I've added/related various other necessary tables to TBL_STORES_Contacts in the query design.

My first problem is when I run the query, not all orders show - coincidentally, the ones missing are those where the contact for Confirmation, Delivery and Invoice are NOT all the same - how can this be fixed?

Secondly, I need to include the fields from TBL_STORES_Contacts 3 times in the query enabling me to display the 3 contact possibilities on the customer report - so the customer can see who receives the confirmation, delivery and Invoice. With the setup Access has given me this is not possible as there's only one table from which to choose 3 different contact possibilities. I've tried adding 2 more instances of TBL_STORES_Contacts (with their associated tables), creating their own relationship line and deleting those 2 Access created at the start of the query. I'm not sure this is the way to go though so wanted to ask before wasting more time than I already have!

Many thanks for your help
Joe

RE: Relating more than one foreign key

Hi Joe

Thank you for your email - we will be back in touch once we have been able to review.

Kind regards,
Andrew

RE: Relating more than one foreign key

Hi Joe

The links to TBL_STORES_Contacts_1 and TBL_STORES_Contacts_2 were created by Access when you added the lookups. These two tables and the links can be removed from the Relationship diagram.

Then create one link from the Contact_ID in TBL_STORES_Contacts to Contact_ID_Delivery and another to Contact_ID_Invoice.

You may need to build the lookups again without using the Lookup wizard. Here are the steps to build the lookups again if you need them.

1. Open TBL_STORES_Orders in design view.
2. Select the Contact_ID_Confirmation field
3. In the Field Properties window at bottom click on the Lookup tab.
4. Change field type from Textbox to Combo Box.
5. Change Row source to TBL_STORES_Contacts

Then do the same thing for the other field Contact_ID_Invoice.

Hopefully that should allow you to set up the query as you want it. Let me know if it works.

Cheers
Doug Dunn
Best STL


RE: Relating more than one foreign key

Hi Doug,

Thank you for the quick reply - I really need to solve this before progressing.

BTW, according to a book I have 'Access 2002: The complete reference' it's normal to have two 'copy' instances TBL_STORES_Contacts_1 & /2 as shown in RelationshipWindow.gif. I'll follow your suggestions however.

Relationship window:
TRIAL 1: I deleted the two 'copy' tables TBL_STORES_Contacts_1 & /2, saved the relationship layout and closed the window.
On opening again, the same two tables were there again with their original relationship line! I deleted them again
TRIAL 2: Now I try to create a relationship between ID_Contact (in TBL_STORES_Contacts) and Contact_ID_Delivery (in TBL_STORES_Orders)
The following message appeared: "A relationship already exists. Do you want to edit the existing relationship? To create a new relationship, click no"
I clicked yes and added the new relationship in the popup pane. A warning appeared saying Access can't enforce referential integrity: I have no choice but to remove referential integrity.
Now there are 3 relationship lines from ID_Contact (in TBL_STORES_Contacts) to TBL_STORES_Orders' fields (Contact_ID_ ... Confirmation, Delivery & Invoice).
I saved the relationship layout, closed then reopened the relationship window.
Now there are 3 more tables appearing: TBL_STORES_Contacts_1/2/3 each with 3 relationship lines from their ID_Contact field - very messy and surely not correct? I managed to delete them all again to get back to just one relationship line from ID_Contact (in TBL_STORES_Contacts) to Contact_ID_Delivery (in TBL_STORES_Orders).
TRIAL 3: I repeated the procedure in TRIAL 2 except after the message from Access I chose 'no' (create a new relationship). I now have 3 relationship lines from ID_Contact (in TBL_STORES_Contacts) to TBL_STORES_Orders' fields (Contact_ID_ ... /Confirmation, /Delivery & /Invoice).
I saved the relationship layout, closed then reopened the relationship window. The layout has been kept!
I had to refuse referential integrity - isn't this a problem?

I examined the Lookup fields in TBL_STORES_Orders - all the Lookups are as before (nothing to change)

Query:
If I create the query again as shown in my first screen shot (QueryDesign.gif) I have the same issues:
1. Not all orders show - the ones missing are those where the contact for Confirmation, Delivery and Invoice are NOT all the same
2. I still need to include the fields from TBL_STORES_Contacts 3 times in the query enabling me to display the 3 contact possibilities on the customer report - so the customer can see who receives the confirmation, delivery and Invoice

Best regards.
Joe

RE: Relating more than one foreign key

H Joe

In the Relationships window you can't remove a table if it is still linked to another table. Delete the links to STORES_Contacts_1 and STORES_Contacts_2 first before removing those 2 tables.

Then create one link from the Contact_ID in TBL_STORES_Contacts to Contact_ID_Delivery and another to Contact_ID_Invoice.

Hope that helps.
I will also test out your method using multiple occurrences if still not achieving what you want.

Regards
Doug
Best STL

RE: Relating more than one foreign key

Dear Doug,

Please see ‘TRIAL 3’ in my above explanation. I believe the result from this is what you wanted me to achieve.
However, referential integrity was removed in the process - isn't this a problem?
Please also see under my title Query:’ for the issues that still remain.

Best regards.
Joe

RE: Relating more than one foreign key

Hi Joe I said I would test out your question. When I did that I had the same issues as you about missing data.

My suggestion is to create separate tables containing Contact_ID_Delivery and Contact_ID_Invoice (and if necessary Contact_ID_Confirmation). Then in your query link to those tables to pull in the contact details for each order.

I'm on holiday for 2 weeks so I personally can't help till my return. However I hope that helps you find a solution for your database.

Regards
Doug Dunn
Best STL

RE: Relating more than one foreign key

Dear Doug,

Before posing my question on the forum, I tried having separate tables in the query (TBL_STORES_Contacts plus the alias _1 & _2 tables). The query was too complicated for Access however (so didn’t work). When I started the query from scratch again, I saw all relationships from the Confirmation, Delivery & Invoice fields all join to the ID of TBL_STORES_Contacts, leading me to believe I’d done something wrong and therefore posed the question.
I’ve now gone back to my original plan but created queries within queries – this works. It appears Access can’t cope with too many tables on its design ‘table pane’, but can produce the same result with tables grouped in other queries.
I guess there’s no more to be learnt this time.
Enjoy your holidays!
Joe


 

Access tip:

Create calculated fields that work out your age

You can uset eh year function to work out the year from NOW function and then subtract it with your date of birth type field

Age=Year(Now( ))-Year([DoB])

View all Access hints and tips


Server loaded in 0.05 secs.