Link to home
Start Free TrialLog in
Avatar of boardtc
boardtcFlag for Ireland

asked on

Key Viloation canceling post in OnBeforePost

Ok, so I have a TDBGrid, in the OnBeforePost event of the table (=TDBGrid.DataSource.DataSet) I ask the user a question, if they say no I cancel the post by calling table.cancel. This line is executed but I then get an EDbEngineError exception saying there is a key Violation, Violation of primary index constraint, cannot insert duplicate key in object table. The record being cancelled does not have a duplicate key anyway. Why is this happening? (I have tried raising an exception, which works, but closes the app.) I just want it to cancel and let the user continue. What am I doing wrong?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

hi tom,

try this in your beforepost-event

If the_User_Said_No then
Begin
  Table1.Cancel;  //Cancel edit or Insert
  Abort;              //Cancel the post
end;

you must abort the post,
because the post goes forward if you don't abort it,
and the db-engine is after the cancel never be able for a post,
which causes in an exception
(the Cancel does not end the post)

meikl
Avatar of boardtc

ASKER

Meikl, cheers, that worked well. How do you recommend that I return the focus of the grid to the first cell, is returns now at the last cell where I pressed tab to go onto the next record (which called the OnBeforePost and resultant cancel). I tried resetting the focus but that has no effect since the grid is already focused, table.first does not work either. Thanks, Tom.
Avatar of boardtc

ASKER

Meikl, please answer to grade. Setting SelectedField worked for the above.

Regards, Tom.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial