Solved

Linked Tables

Posted on 1997-11-13
6
130 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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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