#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Prevent dbgrid update


    Hi,

    I could use some help.

    I am adding new record to table. Add routine is placed in try...except block. Exception handle error if key value is duplicated. But after I hit cancel record i was trying to add is still shown in dbgrid, so i need code to prevent that. tblUnits key is ComplexName + UnitID. Here is my code:

    procedure TfrmAddUnit.spdYesClick(Sender: TObject);
    var
    ButtonSelected: integer;
    Temp: Currency;
    begin
    ...
    try
    frmMain.tblUnits.DisableControls;
    frmMain.tblUnits.Open;
    frmMain.tblUnits.Insert;
    frmMain.tblUnits.FieldValues['ComplexName']:= cboComplex.Text;
    frmMain.tblUnits.FieldValues['UnitNumber']:= edtUnitNumber.Text;
    frmMain.tblUnits.FieldValues['UnitID']:= cboComplex.Text + edtUnitNumber.Text;
    frmMain.tblUnits.FieldValues['Floor']:= edtFloor.Text;
    frmMain.tblUnits.FieldValues['NumOfBeds']:= edtBeds.Text;
    Temp:= StrToCurr(edtPrice.Text);
    frmMain.tblUnits.FieldValues['Price']:= Temp;
    frmMain.tblUnits.FieldValues['Bathroom']:= chkBathroom.Checked;
    ...
    frmMain.tblUnits.Post;
    frmMain.tblUnits.EnableControls;
    Close;
    except
    ButtonSelected := MessageDlg(' You are trying to enter Unit number that already exists' + #13#10 + ' for selected Complex. Do you want to try another value?',mtError, mbOKCancel, 0);
    if ButtonSelected = mrOK then
    begin
    edtUnitNumber.Text:= '';
    edtUnitNumber.SetFocus;
    end
    else
    Close;
    end;

    end;
    I need to change this code so only after everything is OK (no duplicate keys) new record is added. Any suggestions?

    P.S. How to enter tabs in code in forum, looks ugly this way.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    356
    Rep Power
    7
    When you click the Cancel button, you are only clicking the cancel button associated with the MessageDlg. You need to also cancel your database action. Also, your exception will catch all possible errros that can happen in the try section, so your assuming that the error is due to a key violation.

    I would restructure your except block as:
    Code:
    except
       if Pos('duplicate value', E.Message) > 0 then //I think 'duplicate value' is part of the error message?
       begin
          case MessageDlg('You are trying to enter Unit number that already exists for selected Complex. '+slinebreak+'Do you want to try another value?',mtError, mbOKCancel, 0) of
             mrOK:
               begin
                 edtUnitNumber.Text:= '';
                 edtUnitNumber.SetFocus;
               end;
             else 
               begin
                 frmMain.tblUnits.Cancel;//this actually cancels the db operation
                 close;
               end;
          end;
       end
       else
       begin
          MessageDlg('Unknown Error Encountered with Message:'+slinebreak+E.Message, mtError, mbOK, 0);
          frmMain.tblUnits.Cancel;
       end;
    end;
    Last edited by majlumbo; October 25th, 2012 at 09:22 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    That's what i was looking for. Thank you very much.

IMN logo majestic logo threadwatch logo seochat tools logo