0
votes

How can I use DefaultExpression property for TField with MS Access or SQL Server, it seems to work only with BDE.

2

2 Answers

0
votes

Not an exact answer, but it is a workaround. I just call something like the next routine from the AfterInsert event handler of the dataset:

procedure Flds_SetToDefExpr(const AFlds: array of TField);
var
  i : Integer;
begin
  for i := Low(AFlds) to High(AFlds) do
    with AFlds[i] do begin
      DataSet.Edit;
      if ( DefaultExpression='' ) then
        Clear
      else if HasOuterQuotes(DefaultExpression) then
        Value := RemoveOuterQuotes( DefaultExpression,True,False )
      else
        Value := StrToInt(DefaultExpression); //raises if not an integer!
    end;
end;

Ofcourse you could change this routine to accept a TDataSet, which loops it's fields and does the same to all fields.

0
votes

I think that DefaultExpression property dosn't work with ADO, but we get workaround using TBetterADODataSet by Vassil Nazarov that use something like:

Procedure TBetterADODataSet.DoOnNewRecord;
  Var i: Integer;
Begin
  FModifiedFields.Clear;
  For i:=0 To Pred(Fields.Count) Do With Fields[i] Do
    If DefaultExpression<>'' Then Try
      AsString:=DefaultExpression;
    Except
      On E: Exception Do
        ShowMessage(E.Message);
      End;
  Inherited DoOnNewRecord;
End;

You can get it for free here: TBetterADODataSet