Topic: OnSQLException

I am wondering if I can just put the following script of a function in a button click and not call the function
   function OnSQLException(Sender: TObject; Msg: string; SQL: string): boolean;
begin
    // exception from button frmStoixeia.Button4
    if Sender = frmDiafora.Button39 then
    begin
        if Pos('FOREIGN KEY constraint failed', Msg)=1 then
        begin
            result := True; // to prevent system message
            MessageBox('Αδύνατη η διαγραφή. Υπάρχουν συνδεόμενα.', 'Error', MB_OK+MB_ICONWARNING);
        end;
    end;

this function is if we get an error message FOREIGN KEY constraint failed', it should stop the procedure and giving us a custom message.
I want that when i press a button to get the result without  running function. What I want is to create an option with the script running or without the script

2 (edited by brian.zaballa 2022-04-30 21:59:45)

Re: OnSQLException

I'm not so sure if I understand your concern.

Maybe having a constant in the beginning of your script will do.

CONST
    ENV = 'dev';

...
...
...

function OnSQLException(Sender: TObject; Msg: string; SQL: string): boolean;
begin
    if ENV <> 'dev' then
    begin
        // exception from button frmStoixeia.Button4
        if Sender = frmDiafora.Button39 then
        begin
            if Pos('FOREIGN KEY constraint failed', Msg)=1 then
            begin
                result := True; // to prevent system message
                MessageBox('Αδύνατη η διαγραφή. Υπάρχουν συνδεόμενα.', 'Error', MB_OK+MB_ICONWARNING);
            end;
        end;
    end; // end if dev
end;

Then, make sure to make ENV constant to something like 'prod' when you will deploy your application.

If you really want a button in runtime, then you change that CONST to VAR, then have the function toggle the value of ENV.

brian

3 (edited by sparrow 2022-05-01 08:12:21)

Re: OnSQLException

Hi all,
It’s not entirely clear what confuses you, because the function is called automatically
and it has a check for who called it, which button. But...


OnClick - button event before removal,
SQLException is thrown while a delete operation is in progress. 
OnAfterClick - after removal.


You can write your own verification procedure in the OnClick event, for example:
get the number of records associated with your record by ID and if this number is greater than 0, stop further execution with a message.
But this will need to be done for all related tables that interest you.

Re: OnSQLException

Is there any way to bypass this message and delete the record????

Re: OnSQLException

In the OnSQLException handler, raise an exception, and in the event handler, catch it and perform the actions you need.

Визуальное программирование: блог и библиотека.

Re: OnSQLException

Here is your example.
In the Tele table, the properties of the work_id and Customers_id fields are set to cascade delete.
Now, when deleting, there is no error and records are deleted from the Customers and Tele tables without leaving garbage.

Post's attachments

Attachment icon del-3.zip 337.17 kb, 66 downloads since 2022-05-01 

Re: OnSQLException

Yes i know this option in the datsbase. My question is can I do that cascade delete using script ?

8 (edited by sparrow 2022-05-01 13:55:04)

Re: OnSQLException

Brian's suggestion to put NULL in the field. I would suggest deleting entries so as not to leave garbage. Your choice.
Find records in other tables associated with your ID. Delete them if there are any. Delete record with ID without error.

Re: OnSQLException

Thank you all