Page 1 of 1

HELP: TMyQuery, TMyTable set State to Cancel

Posted: Fri 23 Mar 2012 15:41
by ton_cut345
Hi,

I would like to ask help from you guys regarding changing state in TMyQuery or TMyTable

Code: Select all

procedure TFrmCustomers.ADOCustomersBeforePost(DataSet: TDataSet);
begin
  if ADOCustomers.State in [dsInsert, dsEdit] then begin
    if NewRecord = True then begin
      if QuestionMessage('Are you sure you want to add customer: ' + ADOCustomersfirstname.Value + ' ' + ADOCustomerslastname.Value) = mrNo then begin
        ADOCustomers.Cancel;
        NewRecord := False;
      end;
    end else begin
      if QuestionMessage('Are you sure you want to update customer: ' + ADOCustomersfirstname.Value + ' ' + ADOCustomerslastname.Value) = mrNo then begin
        ADOCustomers.CancelUpdates;
      end;
    end;
  end;
end;
everytime questionmessage returns mrNo. TMyQuery should changed its state to cancel but it raise error instead "Duplicate entry '1' for key 'PRIMARY'"

I'm confused since I'm not updating/insterting any data infact I'm cancelling the update/inserting of data.

MyDac version: 5.55.0.37 Delphi 7

Thank you in advance for the help. :)

Posted: Fri 23 Mar 2012 15:59
by AndreyZ
Hello,

The only way to cancel posting in the BeforePost event is to call the Abort method. This functionality is inherited from the TDataSet standard class. For more information, please read the BeforePost event description in the Delphi help.

Posted: Sat 24 Mar 2012 07:59
by ton_cut345
How to call the abort method?

tried this code:

Code: Select all

procedure TFrmCustomers.ADOCustomersBeforePost(DataSet: TDataSet);
begin
  inherited;
if QuestionMessage('Are you sure you want to add customer: ' + ADOCustomersfirstname.Value + ' ' + ADOCustomerslastname.Value) = mrNo then begin
        DataSet.Cancel;
        NewRecord := False;
      end;  
end;
still error message will show
tried this code also

Code: Select all

procedure TFrmCustomers.ADOCustomersBeforePost(DataSet: TDataSet);
begin
  inherited;
if QuestionMessage('Are you sure you want to add customer: ' + ADOCustomersfirstname.Value + ' ' + ADOCustomerslastname.Value) = mrNo then begin
        DataSet.Abort;
        NewRecord := False;
      end;  
end;
there's no abort method.[/code]

Posted: Sat 24 Mar 2012 08:05
by ton_cut345
My only concern here is that I want a confirmation message before posting any data to the database.

Is there another way? or this is the only way, in the beforepost event.

Posted: Sat 24 Mar 2012 08:36
by ton_cut345
Got the answer :D

Code: Select all

procedure TFrmCustomers.ADOCustomersBeforePost(DataSet: TDataSet); 
begin 
  inherited; 
if QuestionMessage('Are you sure you want to add customer: ' + ADOCustomersfirstname.Value + ' ' + ADOCustomerslastname.Value) = mrNo then begin 
        Abort;
        NewRecord := False; 
      end;  
end; 
Any one with the same problem you can call Abort instead TMyQuery.Cancel or Dataset.Cancel

Posted: Mon 26 Mar 2012 07:41
by AndreyZ
It is the correct solution. If any other questions come up, please contact us.