Solved

Master-detail relationship

Posted on 2000-04-08
10
228 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

930 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

19 Experts available now in Live!

Get 1:1 Help Now