Solved

Catching Key Violations

Posted on 1997-07-03
8
1,254 Views
Last Modified: 2008-02-26
I'm trying to write code that cathes key violations in a Paradox table's composite key (the key is made up of two fields).  What code should i use to 1) catch the violation before it happens (maybe onBeforePost or onPostError?) and 2) cancel the record.
0
Comment
Question by:d4jaj1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 5

Accepted Solution

by:
ronit051397 earned 40 total points
ID: 1338156
Usually you catch key violation on the OnBeforePost event.
In there write a code that checks key violation or use try, if there is a key violation, than cancel posting by using the 'Cancel' procedure or the 'Abort' procedure.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338157
Okay, then why am I having problems with the below code?

procedure TfrmActivity.tblActivityBeforePost(DataSet: TDataSet);
begin
  try
    tblActivity.Post;
  except
     on E: EDBEngineError do begin
       if(E.ErrorCount > 0) And
        (E.Errors[0].ErrorCode = DBIERR_KEYVIOL) then
           ShowMessage('You''ve inserted the duplicate record');
    end;
end;
end;

I get an exception on the Key Violation stating "EStackOverflow  with message 'StackOverflow'"  What does this mean and what am I doing wrong in my error code?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338158
Don't use Post on the event 'beforePost', becuese you are creating a kind of "Recursion". Use Post on other events.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:d4jaj1
ID: 1338159
When I take of the line that contains 'Post', doesn't get to the EXECPT block and Delphi stops, probably because the code I have doesn't cath the key violation.  When I pasted the code below to the onPostError, it still wouldn't work.  Do you know the correct code to catch the key violation?

If  E: EDBEngineError then
  if(E.ErrorCount > 0) And (E.Errors[0].ErrorCode = DBIERR_KEYVIOL) then
    table1.cancel;
    ShowMessage('You''ve inserted the duplicate record');
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338160
procedure TForm1.Table1PostError(DataSet: TDataSet; E: EDatabaseError;
  var Action: TDataAction);
begin
  if E.message = 'Key violation.' then Action:=daAbort; //cancel posting
end;

commets:
1. This works for delphi3. I think in delphi2 you should write
'Key Violation' without the dot in the end
2. Set Integrated Debugging to False.

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338161
Excellent, that works!  However, I have other 2 questions (that's why I raised the points.

1.  Why wouldn't this work with the Integrated Debugger ON?  If I cut it off, doesn't that mean Delphi woun't tell me if other errors occur?  Futhermore, the code I entered is in the onPostError - shouldn't that be a reason for Delphi not to stop on its own - because I wrote an exception handler?
2.   My key field is called Name.  A value 'ANIS' already exists.  If I enter 'anis' in lowercase, no keyviolation occurs.  Why and how can I stop this?  I'm using the BDE and Paradox tables.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338162
Answer 1:
The reason you need to set off the integrated debugging is because the abort action is a kind of silence exception.
Here is an explanation from Delphi help:

Delphi applications handle most exceptions that your code doesn't specifically handle by displaying a message box that shows the message string from the exception object. You can also define "silent" exceptions that do not, by default, cause the application to show the error message.
Silent exceptions are useful when you don't intend to handle an exception, but you want to abort an operation. Aborting an operation is similar to using the Break or Exit procedures to break out of a block, but can break out of several nested levels of blocks.

Silent exceptions all descend from the standard exception type EAbort. The default exception handler for Delphi applications displays the error-message dialog box for all exceptions that reach it except those descended from EAbort.
There is a shortcut for raising silent exceptions. Instead of manually constructing the object, you can call the Abort procedure. Abort automatically raises an EAbort exception, which will break out of the current operation without displaying an error message.

Example

The following code shows a simple example of aborting an operation. On a form containing an empty list box and a button, attach the following code to the button's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var  I: Integer;
begin
 for I := 1 to 10 do      { loop ten times }  begin
   ListBox1.Items.Add(IntToStr(I));      { add a numeral to the list }
   if I = 7 then Abort;      { abort after the seventh one }
 end;
end;

try to use the Cancel maethod instead of daAbort, maybe it will work.

Answer 2:
Yes. I am aware of this problem. In my oppinion, you should check this matter before posting a record and by useing this method:

if not Table1.Locate('Name', 'ANIS',[loCaseInsensitive]) then Table1.Post;

The Locate method will locate also 'anis', 'aNiS' etc.

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338163
That's the best answer I've received from this site.  Thank you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question