October 25th, 2012, 04:34 AM
Prevent dbgrid update
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:
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.
October 25th, 2012, 09:18 AM
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:
if Pos('duplicate value', E.Message) > 0 then //I think 'duplicate value' is part of the error message?
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
frmMain.tblUnits.Cancel;//this actually cancels the db operation
MessageDlg('Unknown Error Encountered with Message:'+slinebreak+E.Message, mtError, mbOK, 0);
Last edited by majlumbo; October 25th, 2012 at 09:22 AM.
October 26th, 2012, 03:46 AM
That's what i was looking for. Thank you very much.