Page 1 of 1
MyDAC & C++Builder
Posted: Tue 25 Oct 2005 14:44
by saidus
Hi !!
I have 2 Tables : SALE & DSALE Master / Detail in myisam format
the probleme is that i can't delete the detail rows when i delete the master row even when i use the onbeforepost event for the master Query
(I used 2 TMyQuery components)
so could you help me resolve this ???
thanks for all
Re: MyDAC & C++Builder
Posted: Wed 26 Oct 2005 04:00
by alec
saidus wrote:onbeforepost event
Maybe OnBeforeDelete event
Posted: Wed 26 Oct 2005 08:37
by saidus
yes yes yes
i used & use on before delete event!!but don't work
Posted: Wed 26 Oct 2005 09:51
by GEswin
Perhaps post what you do exactly onBeforeDelete event
Posted: Wed 26 Oct 2005 10:00
by saidus
On the OnBeforePost and OnBeforeDelete event Detail table i poste some information on Master table
Posted: Wed 26 Oct 2005 12:49
by GEswin
on master query you should do something similar to this,
Code: Select all
procedure TFrmPacients.DBQpacfitBeforeDelete(DataSet: TDataSet);
begin
Connection.ExecSQL('delete from detailtable where field = '+BQpacfitcodigo_paciente.AsString,[]);
end;
When you try to delete master field, it deletes details (Of course you have to set correct query).
Posted: Thu 27 Oct 2005 04:53
by alec
More easy way for delete details:
Code: Select all
MyQueryMaster->SQLDelete = "DELETE master, detail FROM master, detail
WHERE master.id = detail.master_id and master.id = :Old_id";
or in designtime
MyQuery editor :: Udate SQLs :: Delete
Code: Select all
DELETE master, detail FROM master, detail
WHERE master.id = detail.master_id and master.id = :Old_id
Note: In this case MyQueryMaster->Options->StrictUpdate must be
false
Posted: Thu 27 Oct 2005 06:23
by Ikar
> i can't delete the detail rows
Please describe it more detailed. Which error message do you get?
Posted: Thu 27 Oct 2005 12:25
by Guest
Hello !!!
I have a memory access error msg !!
the real problem is :
when deleting master record i use this code in OnBeforeDelete event
Code: Select all
TableDetail->First();
while(!TableDetail->Eof)
{
TableDetail->Delete();
}
((( note : MasterTable = TMyQuery , DetailTable = TMyTable )))
but in OnAfterDelete and OnAfterPost detail table i have to calculate some value to store in Master table like this
Code: Select all
Extended tot = 0;
if(MasterTable->State!=dsEdit)
MasterTable->Edit();
DetailTable->First();
while(!DetailTable->Eof)
{
tot+=DetailTableQT->Value;
DetailTable->Next();
}
MasterTableQT->Value = tot;
MasterTable->Post();
thnks for help
Posted: Fri 28 Oct 2005 12:17
by alec
You can use (for MySQL 4.1+):
Code: Select all
SQL: update master set qt = (select sum(detail.qt) from detail where detail.master_id=master.id ) where id= :MasterId
MasterTable->RefreshRecord();
instead of
Code: Select all
Extended tot = 0;
if(MasterTable->State!=dsEdit)
MasterTable->Edit();
DetailTable->First();
while(!DetailTable->Eof)
{
tot+=DetailTableQT->Value;
DetailTable->Next();
}
MasterTableQT->Value = tot;
MasterTable->Post();
and
http://www.crlab.com/forums/viewtopic.php?t=2139#7116
instead of
Code: Select all
TableDetail->First();
while(!TableDetail->Eof)
{
TableDetail->Delete();
}
It must increase perfomance of your application. Sorry for nag

Posted: Mon 31 Oct 2005 15:07
by Ikar
> I have a memory access error msg
You should be very careful on writing event handler that changes data. Do not call Cancel, Post, Next, Delete etc in BeforeX or AfterY event handlers.
If you send us (mydac*crlab*com) a complete small sample we will try to answer more detailed.