tags:

views:

144

answers:

3

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

A: 

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.

MvdH
Thanks MvdH good idea, but there is another easy way, just set CursorLocation to clUseServer for TADOTable and give the fields default value from the server, but I would like to use DefaultExpression property because it except also SQL Expressions, have any body solution?
Kachwahed
A: 

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

Kachwahed
A: 

Does TBetter work with D2010?

Dave
No, you need to convert it.
Kachwahed