About insert records into master & detail table

Results 1 to 2 of 2

Thread: About insert records into master & detail table

  1. #1
    spring.z Guest

    Default About insert records into master & detail table

    I&#039m using Access database. I have two table, a master table named Company, a detail table CompanyUser. The Company table has a autonumber field named CompanyID, which is the primary key. The CompanyUser table also has a CompanyID field, which is a foreign key point to the Company table.<BR> Now, the question is: when the user select to add new data, the flow will like this: insert a new record into the Company table, get the new record&#039s ID, then insert new records to the CompanyUser table using the ID. But, how to get the ID correctly and efficiently? And another issue yet, I want these step will be a whole transaction, How to do? Wish you will understand me(My english is too pool to describe it clear). Thanks!

  2. #2
    Join Date
    Dec 1969

    Default RE: About insert records into master & detail tabl

    I reckon the easiest way to avoid problems with your primary / foreign keys is not to use Autonumber fields - generate a unique ID in script (say the number of milliseconds since 1st Jan 1970 plus a few random digits) and use that as your key. <BR><BR>In order to make this happen in a transaction include the following directive on the first line for the script that processes the insert:<BR><BR>&#060;% @Transaction = required %&#062;<BR><BR>Then, when you&#039ve done you INSERTs check for any errors:<BR><BR>If Err.Number = 0 Then<BR>&nbsp;&nbsp;ObjectContext.SetComplete<BR>E lse<BR>&nbsp;&nbsp;ObjectContext.SetAbort<BR>End if<BR><BR>HTH<BR><BR>Dunc

Posting Permissions

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