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?
boardtcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
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
0
boardtcAuthor Commented:
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.
0
boardtcAuthor Commented:
Meikl, please answer to grade. Setting SelectedField worked for the above.

Regards, Tom.
0
kretzschmarCommented:
good morning tom,

well, glad that this works,
good luck again,

meikl ;-)

puh, i've sleepy eyes, this morning
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.