Relationship between tables ?

Hi All....sorry to ressurrect this thread again but i'm still having trouble with the many-to-many relationship. I've had to leave this project for a while but now i'm trying to finish it. I'm attaching a snapshot of my relationship window below.....what i'm trying to do is create a data input form around my table tblStaffRecords.

I'm going to add a subform to this form which will save the records to the table tblTrainingRecords. The idea is when i select a staff members name from the combo box on the main form, i can then enter a training record on the subform for that person. However i keep getting an error saying i'm violating the primary key rules etc.

I'd appreciate any help,
Thanks Em
 

Attachments

  • Window.JPG
    Window.JPG
    35.8 KB · Views: 116
First question.

What does the Combo Do and does it work properly.

What happens if you use the Navigation buttons to move from one record to the next. (On the Main Form)

Have you ever been able to enter a full record in the sub for.

Is the Primary Key for the Man Form part of its Recordset.

Is the Foreign Key for the sum form part of its Recordset.

Can you create two queries and enter data in both. Only once in the Main and several times in the sum. Making sure the Sub's Foreign key has the same value of on of the Primary keys.
 
BTW

This is a One to Many relationship.

A Many to Many requires an extra table.
 
What does the Combo Do and does it work properly
The source of the Combo Box is a list of staff members and when i choose one, the text box next to it displays that member's ID number. From there, i want to enter a training record into the subform for that person.

What happens if you use the Navigation buttons to move from one record to the next. (On the Main Form)
At the moment it is saying that there's 2 records and it's moving between them


Have you ever been able to enter a full record in the sub form
Yes it seems to accept a record but it doesn't seem to be connecting that record to the person who was selected in the combo box at the time

Is the Primary Key for the Main Form part of its Recordset.
No it's just an autonumber i put in to work as a primary key

Is the Foreign Key for the sub form part of its Recordset
No just another autonumber

Can you create two queries and enter data in both. Only once in the Main and several times in the sum. Making sure the Sub's Foreign key has the same value of on of the Primary keys.
I'm not sure what you mean by this ?

Cheers RainLover








 
Bit by bit. OK

The JPG you sent. I want you to right click on the line between tblTrainingRecords and tblStaffTraining.

Make sure you check "Enforce Referential Intregity"

Do the same with the other two tables. i.e. tblStaffTraining and TblStaffRecords.

Now put two or three lines of data into tblTrainingRecords.. This should create Autonumbers.

In tblStaffTraining enter a few records. Each record MUSt have the PK (Primary Key" from tblTrainingRecords entered into the FK of tblStaffTraining. Forget the PK it will create its own.

You should be able to do this as many times as you like. 100s.

I am assuming the Table tblTraining Records to be your Master. If not just swap every thing I said around.

Please report back and we can move to the next step. About 3 or 4 in all.
 
Bit by bit. OK

The JPG you sent. I want you to right click on the line between tblTrainingRecords and tblStaffTraining.

Make sure you check "Enforce Referential Intregity"

Do the same with the other two tables. i.e. tblStaffTraining and TblStaffRecords.

Done

Now put two or three lines of data into tblTrainingRecords.. This should create Autonumbers.

There's already a few records in there

In tblStaffTraining enter a few records. Each record MUSt have the PK (Primary Key" from tblTrainingRecords entered into the FK of tblStaffTraining. Forget the PK it will create its own.

It won't let me enter records in this table. I'm getting the error message attached

I am assuming the Table tblTraining Records to be your Master. If not just swap every thing I said around.
Does the fact that my data entry form is created around my tblStaffRecords make that my master table ?
[/QUOTE]
 

Attachments

  • Error.JPG
    Error.JPG
    19.5 KB · Views: 112
Emma,

Can you post a jpg of your form?

What are the link fields between MainForm and subform?

The error message in your last jpg indicates, to me at least, you are trying to
create a record for staffTraining but there is no record in the TrainingTable that goes with the value you tried to put in the staffTraining record.
 
Emma,

Can you post a jpg of your form?

What are the link fields between MainForm and subform?

The error message in your last jpg indicates, to me at least, you are trying to
create a record for staffTraining but there is no record in the TrainingTable that goes with the value you tried to put in the staffTraining record.

Emma jdraw is correct. Well at first glance he appears to be correct.

Because you have set Referential Integrity (Google it for more info) it means that if you had the numbers 1 through to say 10 in the Primary Key of tblTrainingRecords, (This should be Autonumber) then in the Foreign Key of tblStaffTraining "TrainingID" you can only have the same numbers. That is the numbers 1 - 10.

Forget about the PK (Primary Key) it will Auto Populate.

I think I said earlier that this is a One to Many. Appologies it is a Many to Many. Sorry about that.

You now need to put some records in tblStaffRecords. The PK of StaffID (Which would be better named StaffRecordID to keep things together) now goes into StaffID.

So now in tblStaffTraining you have its own PK plus the FK of tblTrainingRecords and then the FK of tblStaffRecords.

Hope I am not confusing you here. When I am saying FK for Foreign Key I am saying that in that table it is the FK of which you can have as many as you like and it matches the PK of the other Table.

I am going to attach a sample Database I wrote some time ago. It still should work. It is a Demo in the use of the Many to Many Relationship.

It is a Demo. In a real database you should never show to the public or user either the PK or FK.

There are other things to learn about Keys like Natural Keys. Employee Number, SSN, Membership Number etc. Then there are composite Keys which use two fields to make a unique value. My be advice is to learn about them as you will come across them some day. But in General don't use them. My opinion and some will disagree.

I believe we have found the root cause of your problem so test that out thoroughly before moving to for,.

When you do move to forms make absolutely sure that the PK and FK are part of your Record Source. They do not have to show on the form but they must be part of the Record Source.

Please come back with your problems and keep posting the latest sample of where you are up to.

Another piece of advice is to Google "Access Tutorials" There are some very good ones in stages on UTube.
 

Attachments

jdraw....i've attached an image of the links between my main form and sub form.

RainLover.....first of all i've renamed that field as you suggested. I've cleared the records from my tables to start again and see if i can add a new record. When i select an employee's name from the combo box, their associated employee number appears in the text box beside the combo. However i've also got an employee number text box on the subform but this isn't picking up the same number ?......i thought it would do this if the relationships are set up correctly.

I'm also attaching an updated relationships window snapshot

Thanks for the help guys
 

Attachments

  • Link.JPG
    Link.JPG
    26.6 KB · Views: 113
  • Relationships.JPG
    Relationships.JPG
    35.8 KB · Views: 119
Both should be StaffRecordID for your Master Child links.

Can we leave the following until we get the balance working

"When i select an employee's name from the combo box, their associated employee number appears in the text box beside the combo."

That is unless jdraw is available.
 
Thanks RainLover....i went back into the subform field linker and clicked on the 'Suggest' button. Access reset the links to EmployeeNo and everything started working. I've added a few records and they seem to be going in fine. I think part of the probelm was that i wasn't explaining things very well and also my field names etc should be named a bit better in future.

I appreciate both yourself and jdraw taking the time to help me...i know this isn't easy when you don't have the file in front of you. If i have any more problems i'll report back (and get on your nerves again):)

Thanks again guys
Em xx
 
Well this is embarassing....i'm back already and in trouble again :o

I thought the tables were working fine but there must be still something amiss because when i create a query and run it i'm getting nothing at all. I entered a record as a trial and it turns up in tblTrainingRecords ok, but in tblStaffRecords it turns up twice ?. I have all the tables added to my query but am getting no results when i run it. I've attached a snapshot of the query.

I looked up some info on creating queries on a many to many relationship and one suggested changing the joins to a LEFT join but the i'm getting a message about ambiguous joins....:banghead:

Any ideas what might be wrong ?
Cheers,
Em
 

Attachments

  • Query.JPG
    Query.JPG
    69.8 KB · Views: 107
There is a few thing I should have picked up on earlier.

For a starter the middle table is not required.

Can you post a copy of your Database in Version 2003 so we can have a proper play with it.
 
That's probably the best thing...bear in mind it's still pretty rough.
 

Attachments

That's probably the best thing...bear in mind it's still pretty rough.

Emma

How is it going. Sorry for not being around. I had a little accident and ended in hospital. On the mend now.

Do you need more help or do you have things under control.
 
Oh no...hope you're feeling better. Don't bother with this if you're not feeling well.
No i'm still getting nothing in the query....i've been trying changing things around in the tables but no luck. You mentioned i don't need the middle table ?......how would i get rid of it ?
 
I'm still pretty much stuck at the moment....any ideas anyone ?:confused:
 
I took a look at your database, and here's my two cents - for what it's worth (which, in the current economy is maybe half a penny at the most ;)). Keep in mind, I did glance through most of this thread, but did not read every post in detail, so some of this may have been mentioned before by Rain or jdraw.

If you break this down to its simplest form, you're trying to model a relationship between Staff (employees) and Jobs. Each Staff member needs to be trained on multiple jobs, and each Job can have multiple Staff members trained for it. So, you have the following entities;

1)Staff
2)Jobs
3)Training

and, as I understand things from previous posts in this thread, since each instance of Training may require multiple SOP's, then we can consider SOP's as another entity as well.

Now, as per the description of your model, you have a many-to-many relationship between Staff and Jobs. A many-to-many relationship needs a junction table, which I know has already been discussed here and you have taken under advisement. Where you went wrong (IMO) is when you created a new table (tblStaffTraining) and set it up as a junction table between Staff and Training. First, the m-to-m relationship is between Staff and Jobs, not Staff and Training. Second, you didn't need to create a new junction table to begin with because one already existed (hint: it's the Training table).

Training is the junction between Staff and Jobs. In this case, your Training table happens to need a child table of it's own (SOP's), which is fine. Sometimes that's what the model requires. Now, normally, I would not use a surrogate primary key in a junction table, I would just use a compound key based on the FK fields. However, in cases where a junction table has a child table of it's own, I would use a surrogate key to establish the relationship with the child table (hopefully, my comments on this matter will not throw this thread into some tangential discussion about compound keys vs. surrogate keys, blah blah blah). Actually, this child table (tblTrainedSOPData) is itself a junction table, because you really have another m-to-m relationship here between Training and SOP's.

So, my advice would be for you to try the following and see if it makes more sense for your model. Do this on a test copy of your app of course.

1) Remove tblStaffTraining. It serves no purpose that I can see and is only confusing you.

2) Add the fields StaffID and JobID to tblTrainingRecords. Make them Number data type. Get rid of the EmployeeNo and JobTitle fields you have there now. The only values you want to store here are the PK values from the other tables as FK's. Establish relationships between Staffs/Training and Jobs/Training based on these new fields.

3) Leave the existing TrainingID PK field in tblTrainingRecords. That's your surrogate key.

4) Leave the existing relationship between tblTrainingRecords and tblTrainedSOPData intact, but change the SOPID field in tblTrainedSOPData to Number instead of Autonumber.

5) Add a SOPID (Autonumber) field to tblSOPList and make that the PK, instead of SOPName.

6) Create a relationship between the new SOPID field in tblSOPList and the existing SOPID field in tblTrainedSOPData. Remove the SOPTrained field from tblTrainedSOPData. Again, you only need to store the SOPID, not the name.

7) Make SOPID and TrainingID a compund PK in tblTrainiedSOPData.

Final relationship model might look like;

attachment.php


Note: You may need to remove some existing data from your tables in order to make some of these changes, and you will need to make some adjustments (like record sources and/or Master/Child links) to your forms (or create new forms) when this is done.
 

Attachments

  • Capture.PNG
    Capture.PNG
    27.3 KB · Views: 148
Hi Beetle....thanks for your help. I've rearranged the tables as you suggested. I'm trying to set up a new data entry form to enter people's records but i'm a bit confused because this form will now be based on 5 tables. Would i be right in saying that i should base the main form on tblStaff and then use 4 subforms to get the data into the other tables ?

Thanks again for your help
Em
 

Users who are viewing this thread

Back
Top Bottom