?
Solved

Master-detail relationship

Posted on 2000-04-08
10
Medium Priority
?
232 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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

770 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