Solved

Linked Tables

Posted on 1997-11-13
6
132 Views
Last Modified: 2010-07-27
I would like to have to tables linked.
table1 is the master table.
table2 can have max 1 record for each record in table1 or even no record.


table1                                          table2
-------                                         -------
Id integer      (auto increment)          --->      id integer
x some field                                    x some field

My problem is when i post after a insert, table2 is posted first, but on that moment table1.id has not a value yet and table2 needs the id value.

1) Why is table2 first posted?.
2) Is there any other way to manage my problem.

0
Comment
Question by:gipa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 

Author Comment

by:gipa
ID: 1350292
Edited text of question
0
 

Author Comment

by:gipa
ID: 1350293
Edited text of question
0
 

Author Comment

by:gipa
ID: 1350294
Edited text of question
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 200 total points
ID: 1350295
Gipa,

Try bookmarking the table1 entry, posting it, then issuing a refresh and return to the bookmark. At that point table 2 should be able to read the ID and post correctly.

As an alternative you could have Table1 post it's record then (via a SQL component) query the table1 for it's MAX(ID) value (the latest) and use it as the value in Table2 for the link.

Borland acknowledges the problem with AutoInc fields since version 1.0, but I've not seen a good workaround except for the above examples.

Option 3: If there is something else on the table you can link by other than the AutoInc field then I would suggest doing that :)

Good luck with your project!
Pegasus
0
 

Author Comment

by:gipa
ID: 1350296
this is a part of my source:

procedure TfrmCity.tblAfterPost(DataSet: TDataSet);
var
  oldIndexFieldNames : string;
begin
  if InsertFlag then begin
    oldIndexFieldNames := tbl.IndexFieldNames;
    tbl.IndexFieldNames := 'ID';
    tbl.Last;
    tbl.IndexFieldNames := oldIndexFieldNames;
  end;
  tbl2.FieldByName('ID').AsInteger :=              tbl.FieldByName('ID').AsInteger;
  tbl2.Post;
end;

procedure TfrmCity.tblBeforeDelete(DataSet: TDataSet);
begin
  if Find_Id(tbl.FieldByName('ID').AsInteger) then
    dat2.DataSet.Delete;
end;

procedure TfrmCity.dat2StateChange(Sender: TObject);
begin
  StatusBar.Panels[1].text := DataSetState2String(dat2);
end;

procedure TfrmCity.tblAfterEdit(DataSet: TDataSet);
begin
  InsertFlag := False;
  if Find_Id(tbl.FieldByName('ID').AsInteger) then
    tbl2.Edit
  else
    tbl2.Insert;
end;

procedure TfrmCity.tblAfterInsert(DataSet: TDataSet);
begin
  InsertFlag := True;
  tbl2.Insert;
end;

function TfrmCity.Find_Id(id : integer) : boolean;
var
  LookupResults: Boolean;
begin
  LookupResults := tbl2.Locate('ID', id,[loCaseInsensitive,           loPartialKey]);
  Find_Id := LookupResults;
end;

procedure TfrmCity.tblAfterCancel(DataSet: TDataSet);
begin
  tbl2.Cancel;
end;

I have linked tbl2 to tbl with mastersource and the both id's.
when i post tbl then this  happens only after tbl2 is posted (why?) and so i get the error that tbl2.id should by filled in.


0
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1350297
The problem is that Table 1, while being successfully posted, does NOT "send" the chosen IDENTITY value back to the result set, so even after you post a record to Table 1, the new ID value is not reflected in the data you see on your screen until you issue a Table1.Refresh command.

Your problem most likely is caused by adding a record to table 1 (that didn't exist before the table was opened) then immediately adding a record to table 2. Table 2 looks to get a value for it's ID field but upon looking at table 1, does not find it because the table 1 data is "old" (it's been posted and the server has assigned a value to it's ID field) but your program is not aware of what that number is until you issue a Table1.Refresh in which case it will fetch the CURRENT data (with the ID).

The solution is to dis-allow new Table2 additions until the Table 1 record is posted and Table1 is refreshed. Then you can allow additions to table 2.

If this is not acceptable, then post the table 1 record and use a seperate query object to get the highest ID value from Table 1 to use for the Table 2 ID. Note that this should be done quickly in a multiuser environment to make sure you didn't get someone else's ID number (too many semantics to explain here, just be quick).

Hope this makes since. Delphi is unlike any other language in regards to the way it handles AutoInc fields.

Pegasus

P.S. in your Afterpost code for table 1, before the LAST statement, issue a Refresh to the table to make sure you have a current dataset.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question