Date field automatically filled in as completed date lazarus / 16-Databases ---------------------------------------------------------------------- mangakissa · 2016-01-19 08:22 I put a DBEdit or DBDateEdit on a form. Attach a data source and a data field to which is of type datetime. I want to have complete control over the givven date. I'm using date 26-06-2012 . If I only fill '26', the rest of date will be completed (26-01-2016). Filling '26-06'give the results of 26-06-2016. I want to check mannualy if  the entry is correct. When the data is filled in correctly, age (year / month ) will be  calculated. If the user enters half, I get back a wrong value. How do I get it done to Lazarus ( 1:44 32b / FPC 2.6.4 / Windows 7) to manual check the filled in characters? ---------------------------------------------------------------------- balazsszekely · 2016-01-19 09:02 Quote@mangakissa How do I get it done to Lazarus ( 1:44 32b / FPC 2.6.4 / Windows 7) to manual check the filled in characters? DBDateEdit OnExit event: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses dateutils; procedure TForm1.DBDateEdit1Exit(Sender: TObject);var  BirthDate: TDateTime;  Age: Integer;begin  if TryStrToDate(DBDateEdit1.Text, BirthDate) then  begin    //calulate age    Age := YearsBetween(Now, BirthDate);    ShowMessage('The age is: ' + IntToStr(Age));  end else  begin    //wrong date    ShowMessage('Please enter a valid date!');    DBDateEdit1.SetFocus;  end;end; ---------------------------------------------------------------------- mangakissa · 2016-01-19 10:49 Thanks, Getmem. But it not as simple as it looks. Before the exit the value in DBDateEdit will be filled in as a corrected date. That's my whole point. ---------------------------------------------------------------------- Bart · 2016-01-19 11:25 Are you "jkuiper" in the NLDelphi forum in this thread? The suggestion in OnExit will not work, because the underlying dataset adjusts this field in respons to a WMKillFocus, and only after that the OnExit will be called. You might try OnEditingDone (this comes before OnExit). Bart ---------------------------------------------------------------------- balazsszekely · 2016-01-19 12:08 @Bart You're right! OnEditingDone is a much better choice then OnExit, but since in my code I refocus to DBDateEdit, the user has no choice but reenter the date, even though the field was adjusted. ---------------------------------------------------------------------- mangakissa · 2016-01-19 13:45 Yes, UnEditingDone is better, but the problem relies on function StrToDate/TryStrToDate. The help give me this: QuoteThe Date must consist of 1 to three digits, separated by the DateSeparator character. If two numbers are given, they are supposed to form the day and month of the current year. If only one number is given, it is supposed to represent the day of the current month. (This is not supported in Delphi) The order of the digits (y/m/d, m/d/y, d/m/y) is determined from the ShortDateFormat variable. The function returns True if the string contained a valid date indication, False otherwise. This assumes that 95% of input will return a valid date. All I want is to create an exception if the filled in value is a correct date. Is there another way to check a valid date? ---------------------------------------------------------------------- mangakissa · 2016-01-19 14:26 I've solved it this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmInschrijvingCavias.DBEDateEditEditingDone(Sender: TObject);var datecheck   : string;    checked     : boolean;    correctdate : Tdate;begin  datecheck := DBEDateEdit.Text;  if datecheck > '' then  begin    checked := true;    if length(datecheck) < 10 then      checked := false    else      checked := tryStrToDate(datecheck,correctdate);    if not checked then    begin    showmessage('wrong date');      DBEDateEdit.Text:= '';      DBEDateEdit.SetFocus;    end;  end;end;                                As long as the user hasn't filled a correct datetext (dd-mm-yyyy) it will reult an error. Finally the trystrtodate must validate the foundated date. If all true, the function to calculate age will be called. The result of strtodate is not correct if the filled in partial value will be corrected with the missing parts. For me it's not a solution, but a bug. ---------------------------------------------------------------------- Bart · 2016-01-19 15:16 Another approach: You could specify an EditMask for the DBEdit in question that requires the user to input the right amount of characters in the control. E.g. MyDBEdit.EditMask := '00/00/0000;0;_'  (the "/" will translate automatically to current DefaultFormatSettings.DateSeparator) If the Text does not mactch the mask when control looses focus then an exception will be raised. You then need to have an Application.OnException handler to catch this exception and handle it. Not sure, but you may have te explicitely call ValidateEdit in it's OnEditingDone. All this is untested, of course. Bart