Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 136
  • Last Modified:

Linked Tables

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
gipa
Asked:
gipa
  • 4
  • 2
1 Solution
 
gipaAuthor Commented:
Edited text of question
0
 
gipaAuthor Commented:
Edited text of question
0
 
gipaAuthor Commented:
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!

 
Pegasus100397Commented:
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
 
gipaAuthor Commented:
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
 
Pegasus100397Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now