Link to home
Start Free TrialLog in
Avatar of venks
venks

asked on

Deleting Records in master-Detail tables

I have a master table and a number of Detail tables.When I  delete a record in the
master table i want to simaltaneously delete the relevant child table records.
I tried to use Locate method of Table to find a matching record
and then use Delete method.But it is not executing.
Where am I going wrong?

The code snippet is given below

var
tempser:variant;
searchOptions:TLocateOptions;

Begin
      tempSer:=MasterTable.FieldByName('Ser').AsInteger;
      if detailTable.Locate('Ser',tempSer,searchOptions)
                 then
                         detailTable.Delete;
                                 else
                                     ShowMessage('NO record to be deleted');
end;//of procedure
Avatar of SupWang
SupWang

Why no use the Cascade Update Rule?  Run the DataBase Desktop and search
"Referential Integrity dialog box" in the help.

---------------------------->
Cascade

Cascade specifies that any change you make to the value in the key of the parent table is automatically made in the child table. If you delete a value in the key of the parent table, dependent records in the child table are also deleted. Cascade is the default update rule.
To cascade an update across tables, Database Desktop must place a lock on the target table. If the lock is denied (because another user has already placed a lock), Database Desktop cannot perform the cascade update.

If you are working with SQL tables, the availability of cascading updates and deletes varies according to the table type and software version.
----------------------------<
Avatar of kretzschmar
hi venks,

try as follows
(a faster and better method)

- drop additional a tquery on your form
- select as datasource the datasource of your master
- type in as sqlstatement : delete from detailtablename where ser = :ser

now if you want to delete the derails for the seklected masterrecord you can simple use this code

query1.ExecSQL;
Showmessage(inttoStr(query1.rowsaffected)+' records deleted');

try it

meikl
Avatar of venks

ASKER

Dear Supwang and meikl
Your answer does not tell me how to do the coding>I knew about cascade delete.Cascade delete is used to protect accidental deletion of master table when data is present in child tables.
I took the advice of meikl(using Tquery component) and got the job done.
He deserves the points though he did not answer my question regarding using table methods for deletion.But certainly Tquery is easier to use.
meikl,if you post an answer i will award you the points
Thanks to both of you
venks
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial