How to add records to both tables in a one-to-many relationship in an access database in delphi












-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










share|improve this question




















  • 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 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 at 3:23
















-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










share|improve this question




















  • 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 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 at 3:23














-2












-2








-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










share|improve this question















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







delphi ms-access foreign-keys






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 8:06









GolezTrol

97.6k9130174




97.6k9130174










asked Nov 11 at 20:07









Unknown

1




1








  • 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 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 at 3:23














  • 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 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 at 3:23








1




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 at 20:13




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 at 20:13




1




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 at 3:23




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 at 3:23












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252731%2fhow-to-add-records-to-both-tables-in-a-one-to-many-relationship-in-an-access-dat%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer


























      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.






      share|improve this answer
























        1












        1








        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 8:04









        GolezTrol

        97.6k9130174




        97.6k9130174






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252731%2fhow-to-add-records-to-both-tables-in-a-one-to-many-relationship-in-an-access-dat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation