-2

I am creating a personal trainer type program that will require two tables. In the first table will be the personal details of the client, and in the second all the additional infomation.

I'm using an autonumber primary key and foreign key to connect the two tables. But when I want to add a record to the second table it shows me an error "You cannot add or change a record because a related record is required in table 'Table Name'".

Please help, thank you in advance

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 1
    Welcome to SO. Unfortunately, it is not a tutorial site, and it sounds like you need a basic Master-Detail tutorial. Try googling for one. – MartynA Nov 11 '18 at 20:13
  • 1
    Please show the code you are using to insert records. Are you populating the foreign key when inserting in the second table, and does that key exist in the first table beforehand? – Remy Lebeau Nov 12 '18 at 03:23

1 Answers1

1

You will have to get the id of the row that was just inserted, and use that id as the foreign key in the second table.

How exactly to get that id differs per database. In Access, you can query SELECT @@identity to get that id. You can query it separately, but I think you should also be able to use it in the second insert statement directly, like so:

insert into ChildTable(ParentTableId, othervalue)
values (@@identity, 'Bladiebla');

See also Autonumber value of last inserted row - MS Access / VBA for related information on how to get the id.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210