HELP: TMyQuery, TMyTable set State to Cancel

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ton_cut345
Posts: 13
Joined: Fri 21 Aug 2009 04:12

HELP: TMyQuery, TMyTable set State to Cancel

Post by ton_cut345 » Fri 23 Mar 2012 15:41

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. :)

AndreyZ

Post by AndreyZ » Fri 23 Mar 2012 15:59

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.

ton_cut345
Posts: 13
Joined: Fri 21 Aug 2009 04:12

Post by ton_cut345 » Sat 24 Mar 2012 07:59

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]
Last edited by ton_cut345 on Sat 24 Mar 2012 08:14, edited 1 time in total.

ton_cut345
Posts: 13
Joined: Fri 21 Aug 2009 04:12

Post by ton_cut345 » Sat 24 Mar 2012 08:05

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.

ton_cut345
Posts: 13
Joined: Fri 21 Aug 2009 04:12

Post by ton_cut345 » Sat 24 Mar 2012 08:36

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

AndreyZ

Post by AndreyZ » Mon 26 Mar 2012 07:41

It is the correct solution. If any other questions come up, please contact us.

Post Reply