Solved

Linked Tables

Posted on 1997-11-13
6
133 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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