Board index » delphi » Trapping a Edatabaseerror using TDBGrid

Trapping a Edatabaseerror using TDBGrid

I am using a simple grid with names and numbers, and I have the fields set as required in the database. I would like to know how to trap the errors in a TDBGrid.

To give you some background:

I have defined/created a table using Database Desktop. I am trying to use the TDBGrid as an interface for the user to edit the table. The table contains a Date field, two Alpha fields and several Numeric fields. All of the fields are marked as "required."

Since the fields are "required," if I "clear" any of the grid's cells and press the up or down arrow to select another record (I assume the grid is sending a post event), I get the following error:

Project TEST.EXE raised exception class EdatabaseError with message 'Field' (whichever field is empty) must have a value. Process stopped. Use Step or Run to continue. [OK]
This is what I'm looking to trap. I assumed I would detect this in n error handler and set focus to the field that caused the error.
One additional piece of info: After the above error message, I can press the Run button and get the following dialog box:

Field 'Player' must have a value [OK]
Press OK, and you're back at the record and cell you were editing.
I would like to trap the "EdatabaseError" error, display a similar dialog box, and set focus back to the empty cell. It sounds simple; however, I have been unable to find any examples using a TDBGrid. All of the examples I have seen use a "TEdit" box, assign the value to the field and post to the table using a try .. except.

Is there a way to do what I am attempting?

 

Re:Trapping a Edatabaseerror using TDBGrid


Quote
>>>Project TEST.EXE raised exception class EdatabaseError with message
>>>Field' (whichever field is empty) must have a value. Process stopped. Use
Step or
>>>Run to continue. [OK]

This message is shown only when you start application from Delphi.
If you run your App out of Delphi, you will see only the second message.
You can also go in Tools - De{*word*81} Options - Language Exceptions. Here is
"Stop on Delphi Exceptions" checkbox. Uncheck this box and the first message
will not be shown at all.

Quote
>>>Field 'Player' must have a value [OK]
>>>Press OK, and you're back at the record and cell you were editing.
>>>I would like to trap the "EdatabaseError" error, display a similar
>>>dialog box, and set focus back to the empty cell. It sounds simple;
however,
>>>I have been unable to find any examples using a TDBGrid. All of the
>>>examples I have seen use a "TEdit" box, assign the value to
>>>the field and post to the table using a try .. except.

If you like to show your own message, you can use Application.OnException
event (Delphi5 have ApplicationEvents component).

Here is a sample code:

procedure TForm1.ApplicationEvents1Exception(Sender: TObject;
  E: Exception);
begin
  if not (E is EDatabaseError) // If Exception is not EDatabaseError
    then ShowMessage(E.Message); // Then show default error message
    else
    begin
       // Extra code here or silent exception
    end;
end;

Other Threads