Board index » delphi » TFields.Remove, TFields.Add

TFields.Remove, TFields.Add

If in the code below I move D.Fields.Add(F) after Open the OnCalcFields is
not fired,
value of 'X' is random or error is created.
Why?

The same problem occurs with TQuery so it is not an ADO problem.
Databases tried: SQL Server, Interbase, Sybase, Paradox so it is not
database problem.

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ADODB, Db;

type
 TForm1 = class(TForm)
  C: TADOConnection;
  D: TADODataSet;
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
  procedure DCalcFields(DataSet: TDataSet);
 private
  F: TStringField;
 public
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
D.CommandText:='SELECT * FROM ABC';
F:=TStringField.Create(D);
with F do begin
 FieldName:='X';
 Calculated:=True;
 Size:=3;
 DataSet:=D;
end;
D.Fields.Remove(F);
D.Fields.Add(F);  // there should be only one Add(F)
D.Open;
//D.Fields.Add(F);  // there should be only one Add(F)
while not D.EOF do begin
 ShowMessage(D.FieldByName('X').asString);
 D.Next;
end;
D.Close;
F.Free;
end;

procedure TForm1.DCalcFields(DataSet: TDataSet);
begin
D.FieldByName('X').asString:='X';
end;

end.

======================================================

CREATE TABLE ABC
(
A VARCHAR(3),
B VARCHAR(3)
)

INSERT INTO ABC
(A,B) VALUES ('A','B')

======================================================

Thanks

Marius

 

Re:TFields.Remove, TFields.Add


You can't add fields to an open dataset

Quote
Marius Horak wrote:
> If in the code below I move D.Fields.Add(F) after Open the OnCalcFields is
> not fired,
> value of 'X' is random or error is created.
> Why?

> The same problem occurs with TQuery so it is not an ADO problem.
> Databases tried: SQL Server, Interbase, Sybase, Paradox so it is not
> database problem.

> unit Unit1;

> interface

> uses
>  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
> StdCtrls, ADODB, Db;

> type
>  TForm1 = class(TForm)
>   C: TADOConnection;
>   D: TADODataSet;
>   Button1: TButton;
>   procedure Button1Click(Sender: TObject);
>   procedure DCalcFields(DataSet: TDataSet);
>  private
>   F: TStringField;
>  public
>  end;

> var
>  Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.Button1Click(Sender: TObject);
> begin
> D.CommandText:='SELECT * FROM ABC';
> F:=TStringField.Create(D);
> with F do begin
>  FieldName:='X';
>  Calculated:=True;
>  Size:=3;
>  DataSet:=D;
> end;
> D.Fields.Remove(F);
> D.Fields.Add(F);  // there should be only one Add(F)
> D.Open;
> //D.Fields.Add(F);  // there should be only one Add(F)
> while not D.EOF do begin
>  ShowMessage(D.FieldByName('X').asString);
>  D.Next;
> end;
> D.Close;
> F.Free;
> end;

> procedure TForm1.DCalcFields(DataSet: TDataSet);
> begin
> D.FieldByName('X').asString:='X';
> end;

> end.

> ======================================================

> CREATE TABLE ABC
> (
> A VARCHAR(3),
> B VARCHAR(3)
> )

> INSERT INTO ABC
> (A,B) VALUES ('A','B')

> ======================================================

> Thanks

> Marius

--
Guillermo Casta?o Acevedo
Gerente de Sistemas - Grupo Millennium Ltda
Guiller...@GrupoMillennium.com
http://www.GrupoMillennium.com

Re:TFields.Remove, TFields.Add


Quote
> You can't add fields to an open dataset

He doesn't He has the line commented out.
Quote
> > D.Fields.Remove(F);
> > D.Fields.Add(F);  // there should be only one Add(F)
> > D.Open;
> > //D.Fields.Add(F);  // there should be only one Add(F)

Re:TFields.Remove, TFields.Add


Quote
"Joe Bain" <jb...@addonsystems.com> wrote in message

news:3d36f8aa$1_2@dnews...

Quote
> > You can't add fields to an open dataset

> He doesn't He has the line commented out.

You did not read my question fully.
Error happens when the code looks like this:

D.Fields.Remove(F);
// D.Fields.Add(F);  // there should be only one Add(F)
D.Open;
D.Fields.Add(F);  // there should be only one Add(F)

Marius

Re:TFields.Remove, TFields.Add


"Guillermo Casta?o A" <Guiller...@GrupoMillennium.com> wrote in message
news:3D36C276.67BAFC6D@GrupoMillennium.com...

Quote
> You can't add fields to an open dataset

Yes, I can.

Marius

Other Threads