TDBAdvSmoothDatePicker error on certain strings when inserting to DB

PLEASE UPDATE THE CATEGORY. I could not select VCL | TMS VCL UI PACK because it was marked as read only and I had to pick something.

DateTimeDemo.zip (80.3 KB)

tl;dr: Because TAdvSmoothDatePicker uses VarToDateTime and Data.DB uses StrToDate & StrToTime, some values works at first but fails later, e.g. 57372. Se demo to reproduce.

TDBAdvSmoothDatePicker inherits from TAdvSmoothDatePicker which inherits from TWinControl.

When using Exit events, the order is as follows:

DBAdvSmoothDatePicker.DoExit ->
  -> (inherited) TAdvSmoothDatePicker.DoExit
    -> (inherited) TWinControl.DoExit
      -> (your Exit event if assigned)
        -> continue TAdvSmoothDatePicker.DoExit
          -> continue DBAdvSmoothDatePicker.DoExit
            -> Enter TDateTimeField.SetAsString when assigning field (write to data source).

TAdvSmoothDatePicker.DoExit will try to convert the text value of the component into a TDateTime using VarToDateTime, and if it fails it will change field Date into Now. Changing Date will also update the Text field. If it can parse the string as a TDateTime, the procedure continues without changing any fields.

procedure TAdvSmoothDatePicker.DoExit;
// ...
if (Text <> '') then
begin
  try
    dt := VarToDateTime(Text);
  except
    Date := Now; // SetDate also updates Text
  end;
// ...

VarToDateTime will try to convert the value by first detecting the datatype, which in this case will be string. This will call VarToDateAsString which first tries to convert using a string, and if it fails, tries using a float (important).

function _VarToDate(const V: TVarData): TDateTime;
begin
  case V.VType of
    // ...
    varOleStr:   Result := VarToDateAsString(V);
    // ...
    varString:   Result := VarToDateAsString(V);
    varUString:  Result := VarToDateAsString(V);
// ...

function VarToDateAsString(const V: TVarData): TDateTime;
// ...
VAR_TYPEMISMATCH:
  if not TryStrToDate(S, Result) then
    if TryStrToFloat(S, D) then
      Result := D
    else
      VarResultCheck(VAR_TYPEMISMATCH, V.VType, varDate);
// ...

Next is DBAdvSmoothDatePicker.DoExit which will try to assign the text value to its data source field:

procedure TDBAdvSmoothDatePicker.DoExit;
begin
  inherited;
  if not FInternalCall then
  begin
    if (FOrigValue <> Text) and EditCanModify then
    begin
      if Assigned(FDataLink) then
      begin
        FDataLink.Field.AsString := Text;
        FDataLink.Modified;
      end;
    end;
  end;
end;

FDataLink.Field.AsString := Text;will enter the procedure TDateTimeField.SetAsStringwhich uses StrToDate and StrToTime instead. StrToDate does not use float as a fallback.

procedure TDateTimeField.SetAsString(const Value: string);
var
  DateTime: TDateTime;
begin
  if Value = '' then Clear else
  begin
    case DataType of
      ftDate: DateTime := StrToDate(Value);
      ftTime:
        if Value[1] = '-' then
          DateTime := - StrToTime(Copy(Value, 2))
        else
          DateTime := StrToTime(Value);
    else
      DateTime := StrToDateTime(Value);
    end;
    SetAsDateTime(DateTime);
  end;
end;

Certain strings, e.g.57372, will fail as a string but work as a double. This will cause TAdvSmoothDatePicker.DoExitto continue without changing the value and then fail later on in DBAdvSmoothDatePicker.DoExit.

Do you have an active license for TMS VCL UI Pack as there are normally write permissions in the TMS VCL UI Pack category for everyone with an active license?

I checked this and this isn't based on the latest version of T(DB)AdvSmoothDatePicker that already uses a different date parsing method.
I suggest to first test it with the latest version of TMS VCL UI Pack v13.8.