Solved

Linked Tables

Posted on 1997-11-13
6
128 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
  • 4
  • 2
6 Comments
 

Author Comment

by:gipa
Comment Utility
Edited text of question
0
 

Author Comment

by:gipa
Comment Utility
Edited text of question
0
 

Author Comment

by:gipa
Comment Utility
Edited text of question
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now