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
LVL 1
venksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SupWangCommented:
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.
----------------------------<
0
kretzschmarCommented:
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
0
venksAuthor Commented:
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
0
kretzschmarCommented:
well venks,

glad to helped you and
that you got it work.

supwangs suggestion should also work,
(there is no coding to do,
because that is a definition in the database)
but is not supported by all databases.

good luck

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.