Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I prevent deleting of a record? 1

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA

I need to prevent a record from being deleted and have tried the following code.

procedure TParentDM.IBdsChildBeforeDelete(DataSet: TDataSet);
begin
with ParentDM.IBdsChild do
begin
if ParentDM.IBdsChildC_NO.value > 0 then
ShowMessage ('Delete forbidden.');
end;
end;

The message comes up fine - but, having hit the "O.K." button, I obviously still need something further to prevent the Delete operation from following through.

Meanwhile I get the following exception having hit the "O.K." button.

'violation of FOREIGN KEY constraint "INTEG_1" on table "CHILD"'. Process stopped. Use Step or Run to continue.

Can someone please throw some light on this?

Thanks in advance.

Terry.
 
If you bind a dbnavigator to the dataset, simply hide the delete button. It looks like Interbase is taking care of the process, because you are trying to delete a record that has links to other tables.
If you run the executable file, you will receive the same message with a message box and the program will continue to run, allthough Interbasec will prevent the record of being deleted. Steven van Els
SAvanEls@cq-link.sr
 
Thanks Steven.

Nice to hear from you again. :)

>simply hide the delete button

Unfortunately I need the delete button - for an "authorised" delete.

This problem relates to my thread102-296246 - wherein I created a Master/Detail/Detail/Detail/Detail model (not possible in the normal manner) and which is working perfectly.

Except that I now need to prevent the deleting of a record in (say) table CHILD which still has descendents in table GCHILD.
 
I was away for 2 weeks. You have to delete the corresponding child record first, and if there are grand children attached they have to be removed also.
This is called a cascade delete, to prevent having "orphan" fields and broken relation ships.
If there are no children or grandchildren attached, the delete must work.

For the autorised delete, there are several ways. The fastest one is to grant options to different users. The sysdba or the Owner has all the rights. Make a user with only select and/or update permissions and Interbase will handle the process. The options can be assigned per table or also on a field level.

Another way is according to the user logged in, hide or show the delete button Steven van Els
SAvanEls@cq-link.sr
 
>If there are no children or grandchildren attached, the delete must work.

It does.

But my project isn't a "normal" cascade. So I have to create
my protections against a PARENT record being deleted if there is still a (descendant) CHILD myself.

>Another way is according to the user logged in, hide or show the delete button

NOW you are hitting the nail on the head!

I have already tried the following which works and hides the
entire TNavigator.

if ParentDM.IBdsChildC_NO.value > 0 then
begin
frmFather.nvgtrParent.VisibleButtons := False;
end;

But this won't ..

if ParentDM.IBdsChildC_NO.value > 0 then
begin
frmFather.nvgtrParent.VisibleButtons[nbDelete] := False;
end;

How does one - dynamically - make ONLY the "Delete" button to be False if and when required?

Is this possible?
 
Case closed.

So simple !!

procedure TParentDM.IBdsChildBeforeDelete(DataSet: TDataSet);
begin
with ParentDM.IBdsGChild do
begin
if ParentDM.IBdsGChildGC_NO.value > 0 then
begin
ShowMessage ('Has descendent. Delete forbidden.');
Abort; // :) :) :)
end;
end;
end;
 
Here is some code from the helpfile

look at the construction:

DBNavigator1.VisibleButtons := [nbFirst,nbPrior,nbNext,nbLast];


procedure TForm1.CustomerCompanyEnter(Sender :TObject);

begin
if Sender = CustomerCompany then
begin
DBNavigatorAll.DataSource := CustomerCompany.DataSource;
DBNavigatorAll.VisibleButtons := [nbFirst,nbPrior,nbNext,nbLast];
end
else
begin
DBNavigatorAll.DataSource := OrderNum.DataSource;
DBNavigatorAll.VisibleButtons := DBNavigatorAll.VisibleButtons + [nbInsert,
nbDelete,nbEdit,nbPost,nbCancel,nbRefresh];
end;
end; Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top