Topic: Disable View and Edit of a Record

Hello,
In an application I am working on, the application will read the computer user name using the GetUserName function. This is to be held in a variable and will be written into a field called "Owner" when the user creates a new record.
Once a record is saved, if it has the word "System" as its "Owner" field entry, it should not be able to be opened by pressing the edit button or double clicking the record.
Ideally, if someone tries to edit the record it will display a message box advising that the record cannot be opened.
I can read a field value when the Edit button is clicked but I don't know how to then intercept the process if the field has some particular value.
I hope it all makes sense and any help would be appreciated.
Thanks,
David

Re: Disable View and Edit of a Record

David,
See if the following works for you


procedure Form1_EditButton_OnClick (Sender: string; var Cancel: boolean);
begin
    If SqlExecute('Select Owner From yourTable where id = ' + Form1.TableGrid1.sqlValue) = 'System' then
       Begin
         ShowMessage('System record, cannot open');
         Cancel := True;
       End;
end;

Or you can also hide or disable the edit button when a row is selected that is a system owner record. The following is for a click on a row.

procedure Form1_TableGrid1_OnCellClick (Sender: string; ACol, ARow: Integer);
begin
    If SqlExecute('Select Owner From People where id = ' + Form1.TableGrid1.sqlValue) = 'System' then
      Form1.EditButton.Enabled := False else Form1.EditButton.Enabled := True;
end;

Re: Disable View and Edit of a Record

Hello ehwagner,
thank you so much for all your help with this. Both methods work well. With the second one, I also added your same code to both OnKeyDown and OnKeyUp for the TableGrid so it now works for both using a mouse and for stepping through the records using the up and down cursor keys.
Thanks and regards,
David

4 (edited by derek 2018-01-12 11:55:21)

Re: Disable View and Edit of a Record

Hi David, EHW,
A 'no-script' option (LOL!) that I've used in a couple of applications is simply to put a filter on the grid (so that, in your example, the user never gets to see any records created by 'system' in the first place). 
Obviously, if you want your users to be able to see that the 'system' records exist (even if they can't edit them), it's going to be of less use to you.
In the attached example, I've included both filtered and non-filtered grids for comparison (type in 'ker' for example, in the two search boxes to see the difference).
Regards,
Derek.

Post's attachments

Attachment icon radsoft system user.zip 339.31 kb, 418 downloads since 2018-01-12 

Re: Disable View and Edit of a Record

Hello Derek,
thank you for the ideas, really appreciate it. I have tried it and it is knowledge that I can make use of.
Regards,
David