Dynamically add the primary key to related tables in Access

Posted on 2014-03-21
Medium Priority
Last Modified: 2014-03-24
Hui Experts,

I have about 6-8 tables in an Access 2010 database that relate to the same primary key.  Let's just say that all the tables relate to suppliers and SupplierID is the primary key in all the tables.

When I add a new record to the master table (ie add a new supplier to the Supplier List) I also want the ID to be added in all the other related tables and queries.

How do I do this and what's it called?  Does this have anything to do with "cascade updating".  Also, if I want to add a new table based on the list of all existing SupplierID numbers, do I have to export and re-import as a new table?

I know this is a rookie question.  Thanks for your help.

Question by:thutchinson
  • 3
  • 2
LVL 40

Expert Comment

ID: 39945794
The short answer is that's not how relational databases work.

It is the "foreign key" (primary key of tblA stored in tblB as a pointer back to tblA) that links all the data.  There is no way to know what rows of tblB should contain a new value added to tblA.

I think you are experiencing a problem with queries that is caused by using the wrong type of join.  The default join type is INNER.  That means you are requesting all rows of tblA that have matching rows in tblB.  So, what happens for example if you join Customers to Orders and a Customer has not yet placed an Order?  The recordset will not contain any rows for the new Customer.  If you wanted to see all customers whether or not they had placed an order, you would use a LEFT join.  The left join says - give me all rows in tblA and also any matching rows in tblB.  So, the columns from tblB would exist but be null for any tblA rows without a tblB record.  The third join type is RIGHT and is the same as LEFT but from the other direction.  When you create a query using the QBE and draw the join line, Access pops up a dialog with three options.  Read them carefully and choose the option that will produce the results you are looking for.

If you have defined relationships in the BE by using the Relationships window, Access will automatically draw join lines when you create new queries using QBE but it will use the default join type of INNER so you will have to double click on the join line to change the join type or switch to SQL View and change it by typing.  If you think that the majority of your joins would be LEFT or RIGHT rather than INNER, you can specify a default join type in the Relationship window and when Access automatically creates new joins for you, it will use this default to specify the join type.

Author Comment

ID: 39946380
Yes, I understand how the joins and the relationships work.  I did not express my question well.  I'm so sorry.   Thanks for the thoughtful response.

I need to think about what I'm trying to accomplish here and restate the question.  I will close this an award the points by Monday if I decide to abandon this question.
LVL 40

Expert Comment

ID: 39946501
I forgot to answer the last question.
Also, if I want to add a new table based on the list of all existing SupplierID numbers, do I have to export and re-import as a new table?
Again, this is not how relational databases work.  Select queries and tables are interchangeable for most purposes.  Use queries with selection criteria to isolate subsets of data.  Do not create new tables.  When you create new tables, you will also have to create new forms and queries.

It sounds like you have several 1-1 relationships.  Although there is nothing wrong with that per se, it is unusual so make sure you have created them for the right reasons.  Most people resort to 1-1 relationships because they have too many columns to fit into a single table.  This is a symptom of having one or more repeating groups and the schema should be normalized to remove them.

Have a great weekend.

Author Comment

ID: 39950402
Yes, I have 4 tables with the same primary key (SupplierID) in one-to-one relationships.  Whenever I found that I needed to collect a new type of data, I started a new table.

You are saying that I should have one mega table with many columns.  It makes sense as long as I can remember what I'm storing in all those columns.
LVL 40

Accepted Solution

PatHartman earned 2000 total points
ID: 39950759
One table is easier to work with so unless you need to split off the columns for some particular reason, leave them all together.  Here are some reasons to split.
1. Security - some data is more sensitive than other
2. Sparseness - some data occurs only rarely and so to avoid lots of empty rows, you can put those columns in a separate table.  Since null columns take up only enough room to hold the record lengh field, there is really no technical advantage to this.
3. Some large data fields are used rarely so you want to keep them out of normal processing.

If you have "lots" of columns and keep adding more, be very certain that you are not adding repeating groups.  Those need to go into "many-side" tables.

The Left Join I suggested earlier will solve the problem of working with sparse 1-1 relationships.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question