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!

Edit in grid - No Inserts / Deletes 1

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
This could very well prove to be a very simple question with a very easy answer.
I am making use of a standard TDBGrid component.
I am allowing my users to edit data entries in this grid but want to prevent them from either adding new records or deleting existing ones (I need some degree of control where my users are concerned).
How can this be done ?
I'm guessing this is easily done ?
Thanks in advance
Steve
 
The way I've acheived this in the past is to call the abort procedure in the beforeinsert and beforedelete events of the table eg:-

procedure TForm1.Table1BeforeDelete(DataSet: TDataSet);
begin
abort;
end;

HTH



LeBodge.

"I know what I like and I like what I know"
 
LeBodge - thanks for that.
So far so good. :)
I'm now preventing the addition and deletion of table entries through the grid.
However I now need to allow for the fact that I want to insert and delete entries in the table through code (outside the scope of the user-end DBGrid).
How can I do this now that I've abort'ed the insert/delete actions against the table ?
Ideally I need to be able to insert/delete records through means of code but prevent such actions by the user in the grid component itself. Is this acheivable ?
Further help would be greatly appreciated.
Thanks again
Steve
 
The only way of doing this would be to disable the procedures in which you use the abort command. In this case...

Table1.BeforeDelete := nil;

{Enter your editing code in here before renabling the original procedure}

Table1.BeforeDelete := Table1.BeforeDelete;

Hope this is of use. Arte Et Labore
 
Thanks again.
As an alternative that we've just come up with (talking it through with a colleague always seems to help) would be to make use of two TTable components (pointed to the same target database table) - one (used in the grid) would have a 'do not allow delete' + 'do not allow insert' rule enforced. The other (used in code) would allow inserts and deletes.
Although I would therefore need to ensure that these were kept in sync with one another - refreshing each before doing something with it.
I'm going to give this a shot and see if this gives me the desired results (without too much concern over controlling the situation).
Steve
 
StevenK,

I normally just setup a global boolean variable called say "willabort" and then set this to True or False whenever I need to stop or allow an insert or delete eg:-

procedure TForm1.Table1BeforeDelete(DataSet: TDataSet);
begin
if willabort=True then
abort;
else
{do anything else prior to the record being deleted}
end;

HTH

LeBodge.

"I know what I like and I like what I know"
 
LeBodge - that suggestion makes good sense - I'm trying to implement it.
However I believe I have created myself something of a problem.
Within the DataModule in question I've been making use of the 'DbiSaveChanges()' call behind 'AfterPost' / 'AfterDelete' events of TTable components - in order to do this I have added a reference to the BDE unit in the interface / uses clause.
Now when I comile the code with the 'Abort' behind the 'BeforeInsert' and 'BeforeDelete' events I get the compiler error : "Statement expected, but expression of type 'Integer' found" with the [Error] being highlighted against the 'Abort' call.

Does anyone know what might cause this ?
And furthermore how I can get around it ?

I need both "DbiSaveChanges" ('AfterPost' / 'AfterDelete') and "Abort" ('BeforeInsert' / 'BeforeDelete') - can they share the same unit or is it the fact that I have both 'Before' / 'After' events that's causing the problem here ?

Thanks in advance

Steve.
 
This would be a lot easier if you just used enabled and disabled the procedures (detailed in my previous post) as required when you enter(OnEnter) end exit(OnExit) the relevant DBGrid. I always try to stay away from Abort procedures, seems an untidy way of ending things to me! Arte Et Labore
 
I dont know what you to do this with Abort ?
I think that is better that in onkeydown event you add this code to do this :

Code:
------DBGrid1KeyDown(....)
begin
  if (key = VK_DELETE) and ( ssCtrl in shift ) and (whenIwantToDelete = false) then
       key = 0;
end;

=====
--== Saaket ==--
=====
 
What type of database are you using?
Try to distribute user rights, with relational databases you can grant different rights to users per table or view.
In that way delphi will not allow illegal modifications.

Don't use abort, it can mess things up.

Steven van Els
SAvanEls@cq-link.sr
 
They are Paradox tables so my options are limited. :(
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top