3

I think my Contact List database design is correct, but there is an error. Access 2013 is telling me:

You cannot add or change a record because a related record is required in table 'COMPANY'

I'm new to Access and this is for a class called "Computer Concepts II". I've already done my assignment, and followed the instructions meticulously. This course is held online only and I have tried 3 times in 3 different ways but haven't been able to get any feedback from the instructor.

Here is the breakdown of the things I have tried:

Business Rules

  1. A person must have only one family, but a family can have many people.
  2. A person can work for one or zero companies.
  3. A company can have zero or many people.
  4. For MS Access purposes PERSON is the strong table.

MY ERD Diagram

Eric Hepperle's ERD for Contact List Database in Access 2013

BUILT THE TABLES

  1. Built PERSON table.
  2. Built COMPANY table.
  3. Built FAMILY table.

CREATED RELATIONSHIPS

  1. Created relationships between tables (see screenshot attached). For optionality, set PERSON as the "strong" table.
  2. Originally had ID-FAM and LNAME fields in FAMILY table as both required and indexed.
  3. Originally had ID-PER, ID-FAM_FK, & ID-COM_FK in PERSON table as indexed.
  4. Originally had ID-COM as the only indexed and required field in COMPANY table.

ENCOUNTERED 'RELATED RECORD' ERROR WHILE ENTERING DATA IN PARENT TABLE

    • As indicated in the Using Access 2010 document, I started by entering data in my parent table (PERSON) first. However, when I finished filling all fields in the first row and pressed the "ARROW DOWN" key to advance to the next row, I got an error message saying
"You cannot add or change a record because a related record is required in table 'FAMILY'"

Well, a COMPANY entry is completely optional and not required in any way, so that's a bit confusing. I'm not sure what I'm doing wrong or where to go from here. I did not know how to use Access before this class but I think I have a pretty good handle on it now, and I've followed the instructions meticulously. Thus, I believe the issue is a misconfigured setting somewhere.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • Please edit additional info into your question instead of putting it in a comment. Eg your business rules. Note that your ER diagram does not show that a company is not required for a person. PS There is also [dba.se]. – philipxy Jul 18 '16 at 08:02
  • @philipxy Thanks for the suggestion! I have added the business rules above. Question: Why is my ERD not showing the mentioned relationship? I selected only display equijoin results with PERSON as the strong table. Have I configured the diagram incorrectly, or does Access not have ability to show that relationship, though I've correctly configured it? – Eric Hepperle - CodeSlayer2010 Jul 27 '16 at 13:48
  • 1
    I have tried to repair this question to move the answer to an answer box - we do not use [solved] in titles or edit solutions into questions (especially in the middle). I have stripped out a lot of HTML from this question as it makes questions harder to edit - Markdown is preferred. I have also trimmed thanks, signatures and urgent begging - these are all discouraged. – halfer Aug 01 '16 at 12:38
  • If my posted answer is not an accurate precis of the actual answer, please let me know and we can work together to move solution material to the right place. Thanks. – halfer Aug 01 '16 at 12:39

4 Answers4

4

SOLVED!

I discovered this on my own before seeing Sergey S's comment, but as he pointed out, there should be no value in the "Default" property field. After I discovered the solution I posted the following to my class's discussion board:

It turns out that Access was automatically setting a default value for my foreign keys [FK] of zero ("0"). I didn't realize it, but what was happening was even though a person wasn't required to have a company, because "0" was in the company field for my person record that was what was causing the error. The reason? I suspect that the database was looking for a company record with an ID of "0". Since my company records start at 2 the "0" record was never found, causing the error.

SOLUTION

The way I solved this was simply to set go into the default field for my 2 foreign keys in the design view, delete the zeros, and make sure there was no default values. Or, another way to put it is: ensure there are no default values for your foreign keys.

I was able to add 3-4 rows of data for each table and I even ran a basic query to test the database. It seems to be working perfectly.

Thanks to everyone who contributed!

2

If you don't need to link family and/or company to Person record, do not enter anything to ID-FAM_FK and ID-COM_FK fields, leave them blank. In table definition those fields should not have default value and Required property should be No.

If you need to link Person with Family and/or Company, enter data to Family and/or Company tables first, then add or update record in Person table

Sergey S.
  • 6,296
  • 1
  • 14
  • 29
  • Thanks for your reply. I am not a database expert. I am a beginner, so I don't understand all the terminologies. However, this is my understanding of what I am supposed to accomplish in "buisiness rules" 1) A person must have only one family, but a family can have many people, 2) A person can work for one or zero companies. I believe that is the relationship I set up. I already entered data in Family table first, to no avail. – Eric Hepperle - CodeSlayer2010 Jul 17 '16 at 20:53
  • 2
    Your table structure is right. You should be able to enter and save new record in Person table even if you enter only FNAME and leave the rest of fields empty (BTW, where is Last Name?). If database won't allow to do this, check properties of *_FK fields in table design mode - there should no value in `Default Value` property and `Required` property should be `No` – Sergey S. Jul 18 '16 at 06:22
1

Declaring a FK in a table means that subrow value for its columns in that table must appear in the referenced table. There is no other reason to declare a FK. In SQL databases when a FK column holds a NULL then (by default) the subrow value does not have to appear in the referenced table.

So the non-blank/non-NULL FK values of added or edited Person row must already be in some rows of their referenced tables. In a comment you say a person has to have a family, ie that Person FK is required, ie can be non-blank/non-NULL, but doesn't have to have a company, ie that Company FK is non-required, ie can be blank/NULL. So to add a Person row you must first have a row for their family in Family, then add the Person row with their family's ID-FAM value as ID-FAM-FK and with ID-COM-FK either blank/NULL or the ID-COM value in their company's row in Company.

The error message is not very clear. The actual violation is that before you can leave Person having added or edited rows each row must have a value for every required FK, and hence a row it is referencing, and either that or NULL for every non-required FK. But the error message just mentions the part about an adequate row being missing.

FKs (and other constraints) are not needed to query or update a database, including JOINing. They are for update validation (and enhanced optimization) by the DBMS.

philipxy
  • 14,867
  • 6
  • 39
  • 83
0

Go to Database, then Edit Relationship that is in between two related tables. Check "Cascade Update Related Field". Now an update or edit will work and no need to update anything else.