Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBEDIT - DATE FIELD ?

Status
Not open for further replies.

selena1

Programmer
Apr 7, 2003
69
I have a Paradox table that have fields: ID, Name, HireDate, BirthDate......... When I set some value for date in field HireDate, which isn' correct, and then try to exit component dbEdit, I get a message :"Illegal input value" or "EConversion Error - '...' is not valid date". That happens before I try to post or update record. I have set an input mask : "!99/99/00/;0;_". How can I change a message that I get (I wont to get my message on some other language, not English)? Also, can anybody tell me if I have more than one required fields or more than one date fields in a record, how I can get inoformation which field caused post error or update error. In this case I can change message, setting my own message in OnPostError and using command "abort" after that.
Thanks!
 
You have several places to do validation:
OnChange of the DBEdit (not very useful)
OnSetText of the TField
OnValidation of the TField
BeforePost of the TDataSet

If you want to display your own message about the date format, use OnSetText of the TField:
Code:
procedure TForm1.TableBirthdateSetText(Sender: TField; const Text: String);
begin
   try
       Sender.AsDateTime := StrToDateTime(Text);
   except
       raise EConvertError.Create('My Error Message');
   end;
end;
///////////////////////////////////////////////

You can customize the display name of a field by changing the .DisplayLabel of the TField. This is the name displayed by the "required" error message (and also the default name in DBGrid column headers).

If you want to change the entire "required" error message, you either have to change the resourced error message string (SFieldRequired in DBConsts.pas) or test the required fields yourself:
Code:
procedure TForm1.Table1BeforePost(ADataSet : TDataSet);
var
   Counter : Integer;
begin
   with ADataSet do
   begin
       for Counter := 0 to FieldCount - 1 do
       begin 
           with Fields[Counter] do
           begin
               if Required and (not ReadOnly) and (FieldKind = fkData) and IsNull then
                  raise Exception.Create('My Error Message on ' + DisplayName);
           end;
       end;
   end;
end;
end;

Cheers
 
I have have tried some of your advices and I had some trouble.
First, I have used "OnSetText" event and worked fine but there is a trouble if field "BirthDate" had some value and later I wonted to clear this value and save changes (empty string). I always get message: "My Error Mesage". Can you help me with this?
Also, after I open DBConst.pas and change SFieldRequired Message I still continue to get old message on english. I have got changed message few times when I started my application from Delphi IDE, but when I save application and started it as .exe file I get old message. Where is the trouble?

THANKS FOR YOUR PREVIOUS ANSWER!
 
Code:
procedure TForm1.TableBirthdateSetText(Sender: TField; const Text: String);
begin
   if Text = '' then
       Sender.Clear
   else
   try
       Sender.AsDateTime := StrToDateTime(Text);
   except
       raise EConvertError.Create('My Error Message');
   end;
end;

///////////////////////////////////////

You can change 'resourcestring' stuff in the EXE after it is created (e.g. with ResHack), but to force it to change at compile-time, you have to delete the DBConsts.DCU in order to force your changes to take effect. THIS IS A MAJOR STEP, but it will work. I cannot see any real bad consequences, except that if you try to compile your code on any other system that has the old DCU, you will see the same bad behavior in your EXE.

Perhaps the Borland site has a TI on how to changes these messages.

Cheers
 
I still have trouble. If I have understanded you correctly I have done this:
1) Open DBConsts.pas, change ResourceString and save changes;
2)Delete DBConsts.dcu from C:\Program Files\Borland\Delphi7\Lib;
3)Make my project, that use DB components;
4)Run my project;
When I try to run my programm first time I get a message: "DBConsts.dcu file didn't find". Or something like that.
Maybe I should include "DBConst.pas" unit in my project and than try to compile project? Where is mistake?
THANKS!
 
"Maybe I should include "DBConst.pas" unit in my project and than try to compile project?"

Yup.

And it was My Bad to tell you to delete the DCU. Next time, just rename it to *.DCUOLD.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top