Solved

Master-detail relationship

Posted on 2000-04-08
10
226 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

18 Experts available now in Live!

Get 1:1 Help Now