procedure SetDataActive(ds: TCustomADODataSet);
var
  i: Integer;
begin
  with ds do
  begin
    DisableControls;
    try
      Close;
      Connection := DataModule.cnnErp;
      for i := 0 to Fields.Count - 1 do
        if Assigned(Fields[i].LookupDataSet) then SetDataActive(TCustomADODataSet(Fields[i].LookupDataSet));
      Open;
    finally
      EnableControls;
    end;
  end;
end;procedure RefreshDataSet(ds: TCustomADODataSet);
var
  intRec: Integer;
begin
  if ds.ClassType = TADOTable then
  begin
    intRec := ds.RecNo;
    TADOTable(ds).Requery;
    if ds.RecordCount > 0 then ds.RecNo := Min(intRec, ds.RecordCount);
  end else
  begin
    with ds do
    begin
      if not Active then exit;
      intRec := RecNo;
      DisableControls;
      try
        Close;
        Open;
        if RecordCount > 0 then RecNo := Min(intRec, RecordCount);
      finally
        EnableControls;
      end;
    end;
  end;
end;