Board index » delphi » Exception handling DBEDIT (TDateTime)

Exception handling DBEDIT (TDateTime)

Hello everybody,

I have attached a dbedit to a datetime field of a TTable.
When I fill in something incorrect  and try to leave the TDBEDIT I
want to catch  the exception myself and display a more user friendly
message instead of the EConvertError " xxxx is not a valid date ". I
want to generate a message in another language.

This exception seems to be generated before I can trap it with my own
exception handling code. I have tried to catch it in several places
but this message is always diplayed first, and after that my own
message appears.

Does anyone know the best way to trap this exception and where to do
it ?

Thanks,

Steve

 

Re:Exception handling DBEDIT (TDateTime)


Have you tried the TField's OnValidate event?

Quote
scc...@worldonline.nl wrote:

> Hello everybody,

> I have attached a dbedit to a datetime field of a TTable.
> When I fill in something incorrect  and try to leave the TDBEDIT I
> want to catch  the exception myself and display a more user friendly
> message instead of the EConvertError " xxxx is not a valid date ". I
> want to generate a message in another language.

> This exception seems to be generated before I can trap it with my own
> exception handling code. I have tried to catch it in several places
> but this message is always diplayed first, and after that my own
> message appears.

> Does anyone know the best way to trap this exception and where to do
> it ?

> Thanks,

> Steve

Re:Exception handling DBEDIT (TDateTime)


On Wed, 12 Mar 1997 13:57:34 -0500, "David M. Goncalves"

Quote
<dmgon...@vicon.net> wrote:
>> Hello everybody,

>> I have attached a dbedit to a datetime field of a TTable.
>> When I fill in something incorrect  and try to leave the TDBEDIT I
>> want to catch  the exception myself and display a more user friendly
>> message instead of the EConvertError " xxxx is not a valid date ". I
>> want to generate a message in another language.

>> This exception seems to be generated before I can trap it with my own
>> exception handling code. I have tried to catch it in several places
>> but this message is always diplayed first, and after that my own
>> message appears.

>> Does anyone know the best way to trap this exception and where to do
>> it ?

You have two choices...
The first is listed above on the validation event. I prefer the
second...

Use a Edit rather than a DBEdit box (this allows you to enter ANYTHING
without field type considerations). In the ONEXIT event, use a
try/finally block

var
    dateTester:TDateTime;

     TRY begin
        dateTester(Edit1.Text);
        table1.FieldByName('DateField'):=
             Edit1.Text;
        end;
     EXCEPT ON EConvertError do begin
       ShowMessage(' Can't you read? I said enter a DATE jerk!'+#13
+'Geezz and you run a computer? ');
       Edit1.SetFocus;
       exit;
    end;

(I think this will work, procedurally) You might choose a different
ShowMessage text but you get the idea...   :)

The EConvertError will trap the conversion immediately on the
DateTester(Edit1.Text) attempt to convert the Edit1.Text to a proper
date/time value. If there is an error, all following code is bypassed
and the routine jumps to the EXCEPT coding. I use an EXIT routine to
break out of the procedure and through the user back into the date
entry field. I use an alpha-numeric field instead of DATE fields for
this reason. I like to trap them using program code rather than
expecting the database to do it and end up corrupted. It's really easy
(and EXTREEMLY flexible) to manipulate text rather than structured
table definitions!

Good luck, hope this is of some help!

Bill
--------------------------------------
arte...@ix.netcom.com

There are three kinds of people in the world...
     Those that can count and those that can't!

Other Threads