dcsimg
Results 1 to 4 of 4

Thread: Error 3201: You can't add or change a record because a related record is required

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Error 3201: You can't add or change a record because a related record is required

    Hi. I have two tables, one called tblExpungements and one called tblProsecutors.

    tboProsecutors has primary key called ProsecutorID, then other fields such as ProsecutorFName, ProsecutorLName, etc.

    tblExpungements has a foreign key called ProsecutorID which is linked to the tblProsecutor table.

    I have a form based on tblExpungements. There is a drop down box on this form for the foreign key field: ProsecutorID which looks up the Prosecutor's name in the tblProsecutors table.

    Everything works fine when you fill in the form completely and save it. However, the problem I'm having is when someone starts to fill in the form, but decides to delete it before all fields are filled in. If the ProsecutorID combobox field hasn't been filled in yet, I get the following error when trying to delete the record:

    Error 3201 You can't add or change a record because a related record is required in table tblProsecutors.

    So in order to delete the record, you have to first choose a prosecutor from the combobox, then you can delete the record.

    The field is set to Not Required...so why won't it let me delete the record unless I fill in this field? I figure I could trap the error and have the code me.undo run before it deletes it...but it doesn't seem right to have to do this. I might not want to undo everything if the error arises because of a different reason.

    I scoured all kinds of forums, but people seem to usually get this error when they are trying to save a record but there is some sort of referential parent/child problem. Not when they are just trying to delete the record. I believe my relationships are sound. Let me know if you need any clarification on the problem. Thanks for your help.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: Error 3201: You can't add or change a record because a related record is required

    It sounds like you're still trying to save the partial data when the user cancels out. You're trying to save the child record when there's no parent record. If you have a foreign key relationship then the parent record must always be inserted first, before the child record. When deleting the opposite is true, i.e. the child must be deleted before the parent. You simply cannot have an orphan child record.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: Error 3201: You can't add or change a record because a related record is required

    I'm not sure how it would be trying to save the partial record. The process is: they start a record, fill in a couple of the fields...decide they didn't mean to enter this record...hit DELETE...then I get the error message. How would it be trying to save the record, when they are clicking delete? Is there something I'm missing?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: Error 3201: You can't add or change a record because a related record is required

    Nevermind...figured it out. The foreign keys had a default value of 0 (the default when creating a table). When I deleted the default value out of the table design, the record now seems to delete fine without the error. Although this default value of "0" wasn't displayed in the combobox on a new record, it must have been trying to assign that to the foreign key somehow?...and there wasn't a matching record of "0" in the tblProsecutorsTable. I think that's why, not exactly sure...but it works.
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width