Solved

Master-detail relationship

Posted on 2000-04-08
10
231 Views
Last Modified: 2010-04-04
Hi,

I have database app that has a master table with several detail tables. The link is through the a field called "title".

When I edit this the title in the master table, how do I make it so that the change of title is reflected in the detail tables?

At the moment when I change the title in the master table, a new record is added in the detail tables rather than the old record being modified. This causes data associated with the old title to become "lost".

Thanks

John
0
Comment
Question by:JohnE
[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
10 Comments
 

Expert Comment

by:efj67
ID: 2697399
I'm not sure I understand. Let me get this, you have a relational link with several child tables, but when you change the linking data you want the change to be reflected in the child tables? You will have to do some coding to accomplish this.

1. First you want to capture the linking data as it is presently in a variable.

2. Put the new info in a variable also.

3. Then use a decision statement to change data records in the child tables.

Your code might look something like this

var

oldinfo : <datatype>
newifo : <datatype>

begin
editnewinfofield.text:=newinfo;
oldinfo:=dbeditfield.value;
firstchildtable.open;
firstchildtable.first;
while firstchildtable not eof
 do
  begin
   if firstchildtablefield=oldinfo then
   begin
    firstchildtablefield.vaule:=newinfo;
    firstchildtablefield.post;
   else
    firstchildtablefield.next;
   end;
end;
end.


Note you may want to add some safety features in this code to insure that the integrity of the tables is maintained. If you are planning to network this app, this should be an offline batch operation.

I do realize this is rather bland but it should get the brain cooking.

Feel free to e-mail if I am off target.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2697435
for what database you need this?
0
 

Author Comment

by:JohnE
ID: 2697519
Initially it was for paradox tables, but I have migrated to DBISAM.
0
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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2704718
hi johne,

i am not familiar with dbisam, therefore i don't know,
if there could be done a constraint for modify forwarding at databaselevel.

in this case, you can(must) it do in the way, which  efj67 suggested, also if there are errors ( efj67 you've forgotten to edit the detailtable)

meikl
0
 

Author Comment

by:JohnE
ID: 2707616
Hi,

Below is some code based on what you suggested. This works, but only for the first instance of the title occuring in the secondary table.

If the title occurs more than once, only one instance is converted to the new title.

How can I make all instances change?

Table 1{master table}
Table 2{detail table}
Table 3{same table as 2, but without m/d link}

Thanks

John

procedure TForm1.UpdateClick(Sender:
TObject);
var
   oldinfo : string;
   newinfo : string;
begin
     Table1.Edit;
     Table2.close;
     Table1.post;
     newinfo := DBEdit1.text;
     oldinfo:= Edit1.Text;
with Table3 do      
begin
          first;
          open;
          edit;
          FindKey([Table1.FieldbyName('Title').AsString]);
          begin
          while not Eof do
            begin
                if Fields[0].AsString =oldinfo then
                   begin
                       Fields[0].AsString:=newinfo;
                       post ;
                       next;
                    end
            else
                next;
                Edit;
            end;
          end;
    end;
   Table2.open;
end;
0
 

Author Comment

by:JohnE
ID: 2707666
Hi,

Below is some code based on what you suggested. This works, but only for the first instance of the title occuring in the secondary table.

If the title occurs more than once, only one instance is converted to the new title.

How can I make all instances change?

Table 1{master table}
Table 2{detail table}
Table 3{same table as 2, but without m/d link}

Thanks

John

procedure TForm1.UpdateClick(Sender:
TObject);
var
   oldinfo : string;
   newinfo : string;
begin
     Table1.Edit;
     Table2.close;
     Table1.post;
     newinfo := DBEdit1.text;
     oldinfo:= Edit1.Text;
with Table3 do      
begin
          first;
          open;
          edit;
          FindKey([Table1.FieldbyName('Title').AsString]);
          begin
          while not Eof do
            begin
                if Fields[0].AsString =oldinfo then
                   begin
                       Fields[0].AsString:=newinfo;
                       post ;
                       next;
                    end
            else
                next;
                Edit;
            end;
          end;
    end;
   Table2.open;
end;
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2707702
hi johne,

did you thought about to use a query
(if possible with dbisam)

the statement is like

Update YourDetailTableName Set YourFieldName = YourNewValue Where YourFieldName = YourOldValue

and fire the statement with the query.ExecSQL-method

its much less code(if parametewrs are used) and its faster than iterating through the table

meikl
0
 

Expert Comment

by:floriangrimm
ID: 2707881
thats really good idea!!!
0
 

Author Comment

by:JohnE
ID: 2711198
Meikl,

Your SQL suggestion worked perfectly; and you're right - half the code and heaps faster.

How do I give you the points?

Thanks for your help.

John
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2711243
hi johne,

>How do I give you the points?
you've just done it

glad that you get it work
thanks for accepting my comment as answer
good luck again

meikl ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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