How to ensure a date entered is correct, and if not then fix it.
function FixDate(YYYY, MM, DD: Word): TDateTime;
begin
if DD < 1 then DD := 1;
if MM < 1 then MM := 1;
if YYYY < 1 then YYYY := 1;
Result := -1;
while DD > 0 do
try
Result := EncodeDate(YYYY, MM, DD);
//It worked, we can stop now
Break;
except
//Date was not valid, try the day before
Dec(DD);
end;
end;