Tfield em tempo de execução
Colegas,
Como eu crio Tfields em tempo de execução?[size=18:0e03c800bf][/size:0e03c800bf]
Como eu crio Tfields em tempo de execução?[size=18:0e03c800bf][/size:0e03c800bf]
Muxagata
Curtidas 0
Respostas
Fabio.hc
03/06/2004
Tente assim:
procedure TForm1.btnCriaFieldClick(Sender: TObject); var T: TStringField; begin if qryCliente.Active then qryCliente.Close; T := TStringField.Create(Self); T.fieldName := ´cli_Nome´; T.FieldKind := fkData; T.DisplayLabel := ´Nome do Cliente´; T.Visible := True; T.Name := qryCliente.Name + T.FieldName; T.Index := qryCliente.FieldCount; T.DataSet := qryCliente; qryCliente.FieldDefs.UpDate; qryCliente.Open; end; procedure TForm1.btnApagaFieldClick(Sender: TObject); var TC: TComponent; begin TC := FindComponent(´qryClientecli_Nome´); if not (TC = nil) then begin qryCliente.Close; TC.Free; qryCliente.Open; end; end;
GOSTEI 0
Adilsond
03/06/2004
Exemplo pego em um forum. Não lembro quem enviou:
var fCampo : TStringField; fNome : TStringField; fCodigo : TFloatField; fFloat : TFloatField; fData : TDateField; implementation procedure TForm1.Button1Click(Sender: TObject); begin // Fecha a query Query.Close; // Limpa o sql Query.SQL.Clear; // Cria o sql Query.SQL.Add(´Select(codigo, nome from funcionarios´); // Campo nome if not Assigned(fNome) then begin fNome := TStringField.Create(Self); fNome.FieldName := ´nome´; fNome.FieldKind := fkData; fNome.Size := 30; fNome.DisplayWidth := 30; fNome.DataSet := Query; end; // Campo Código if not Assigned(fCodigo) then begin fCodigo := TFloatField.Create(Self); fCodigo.FieldName := ´codigo´; fCodigo.FieldKind := fkData; fCodigo.DisplayWidth := 10; fCodigo.DataSet := Query; end; // Campo Calculado if Assigned(fCampo) then begin fCampo := TStringField.Create(Self); fCampo.FieldName := ´Inicial´; fCampo.FieldKind := fkCalculated; fCampo.Alignment := taCenter; fCampo.Size := 1; fCampo.DisplayWidth := 1; fCampo.DataSet := Query; end; // Campo Calculado if Assigned(fFloat) then begin fFloat := TFloatField.Create(Self); fFloat.FieldName := ´Mult´; fFloat.FieldKind := fkCalculated; fFloat.Alignment := taCenter; fFloat.DisplayWidth := 10; fFloat.DataSet := Query; end; // Campo Calculado if Assigned(fData) then begin fData := TDateField.Create(Self); fData.FieldName := ´Data´; fData.FieldKind := fkCalculated; fData.Alignment := taCenter; fData.DisplayWidth := 10; fData.DataSet := Query; end; // Abre a Query... Query.Open; // Cria as colunas (OPCIONAL) usado para criar os titles. with Grid.Columns do begin Items[0].Title.Caption := ´Cód.´; Items[1].Title.Caption := ´Nome´; Items[2].Title.Caption := ´Letra Inicial´; Items[3].Title.Caption := ´2x Cód.´; Items[4].Title.Caption := ´Data´; end; end; procedure TForm1.FormDestroy(Sender: TObject); begin // Se o campo foi criado tira-o da memória if Assigned(fCampo) then FreeAndNil(fCampo); if Assigned(fNome) then FreeAndNil(fNome); if Assigned(fCodigo) then FreeAndNil(fCodigo); if Assigned(fFloat) then FreeAndNil(fFloat); if Assigned(fData) then FreeAndNil(fData); end; procedure TForm1.QueryCalcFields(DataSet: TDataSet); // <------ Preste atenção para criar este evento!! begin Query.FieldByName(´Inicial´).asString := Copy(Query.Campo(´nome´),1,1); Query.FieldByName(´mult´).asFloat := Query.GetInteger(´codigo´) * 2; Query.FieldByName(´data´).asDateTime := Now; end; procedure TForm1.FormShow(Sender: TObject); begin Query.Select(´codigo,nome from funcion´); with Grid.Columns do begin Items[0].Title.Caption := ´Cód.´; Items[1].Title.Caption := ´Nome´; end; end;
GOSTEI 0
Cabelo
03/06/2004
Caros Colegas..
Se vc´s puderem me ajudar.. Estou ´pastando´ com esse problema já faz tempo. É o seguinte, preciso criar um Tfield em RUN TIME, por quê ele faz parte de um componente que estou criando..
s_field := TStringField.Create(self);
s_field.FieldName := ´D´ + copy(l_campo, 2, length(l_campo) - 1);
s_field.FieldKind := fkLookup;
s_field.Size := 1;
s_field.LookupDataSet := l_lookup;
s_field.LookupResultField := l_result_field;
s_field.LookupKeyFields := l_key_field;
s_field.DataSet := l_dataset;
onde passo parâmetros para esta procedure..
o problema é que está dando o seguinte erro:
´Lookup Information for field D_NIVEL is incomplete.´
Vc´s poderiam me ajudar, já tentei de tudo e não tive êxito..
agradeço a atenção.
Cabelo
Se vc´s puderem me ajudar.. Estou ´pastando´ com esse problema já faz tempo. É o seguinte, preciso criar um Tfield em RUN TIME, por quê ele faz parte de um componente que estou criando..
s_field := TStringField.Create(self);
s_field.FieldName := ´D´ + copy(l_campo, 2, length(l_campo) - 1);
s_field.FieldKind := fkLookup;
s_field.Size := 1;
s_field.LookupDataSet := l_lookup;
s_field.LookupResultField := l_result_field;
s_field.LookupKeyFields := l_key_field;
s_field.DataSet := l_dataset;
onde passo parâmetros para esta procedure..
o problema é que está dando o seguinte erro:
´Lookup Information for field D_NIVEL is incomplete.´
Vc´s poderiam me ajudar, já tentei de tudo e não tive êxito..
agradeço a atenção.
Cabelo
GOSTEI 0
Adilsond
03/06/2004
Para um TStringField voce deverá informar os seguintes campos:
FieldName = String
LookupDataSet = TTable/TQuery
LookupKeyFields = String
LookupResultField = String
KeyFields = String
Size = Integer
Lookup = True
FieldName = String
LookupDataSet = TTable/TQuery
LookupKeyFields = String
LookupResultField = String
KeyFields = String
Size = Integer
Lookup = True
GOSTEI 0
Cabelo
03/06/2004
Caro Adilson
Fiz exatamente como o quê vc me passou, o erro que dá agora é que meu select retorna dizendo que o campo lookup não existe..
´Field D_NIVEL not found´.
Não sei o pq??
se colocar claculated := True, aí funciona blz, mas não cria o lookup no grid, e aparece os dados no grid em branco.
O quê estou fazendo errado??
with s_field_d_nivel do
begin
FieldName := ´D´ + copy(l_campo, 2, length(l_campo) - 1);
Index := l_dataset.FieldCount;
FieldKind := fkLookup;
Size := 1;
Visible := true;
SetFieldType(ftString);
Lookup := true;
LookupDataSet := l_lookup;
LookupResultField := l_result_field;
LookupKeyFields := l_key_field;
KeyFields := s_field_c_nivel.FieldName;
DataSet := l_dataset;
//Calculated := true;
end;
Agradecido por sua atenção
Cabelo
Fiz exatamente como o quê vc me passou, o erro que dá agora é que meu select retorna dizendo que o campo lookup não existe..
´Field D_NIVEL not found´.
Não sei o pq??
se colocar claculated := True, aí funciona blz, mas não cria o lookup no grid, e aparece os dados no grid em branco.
O quê estou fazendo errado??
with s_field_d_nivel do
begin
FieldName := ´D´ + copy(l_campo, 2, length(l_campo) - 1);
Index := l_dataset.FieldCount;
FieldKind := fkLookup;
Size := 1;
Visible := true;
SetFieldType(ftString);
Lookup := true;
LookupDataSet := l_lookup;
LookupResultField := l_result_field;
LookupKeyFields := l_key_field;
KeyFields := s_field_c_nivel.FieldName;
DataSet := l_dataset;
//Calculated := true;
end;
Agradecido por sua atenção
Cabelo
GOSTEI 0
Adilsond
03/06/2004
Veja o exemplo que fiz:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ExtCtrls, DBCtrls, Grids, DBGrids, DBTables;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
Table2: TTable;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
Table2VendorNo: TFloatField;
Table2VendorName: TStringField;
Table2Address1: TStringField;
Table2Address2: TStringField;
Table2City: TStringField;
Table2State: TStringField;
Table2Zip: TStringField;
Table2Country: TStringField;
Table2Phone: TStringField;
Table2FAX: TStringField;
Table2Preferred: TBooleanField;
Table1PartNo: TFloatField;
Table1VendorNo: TFloatField;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Table1VendorName: TStringField;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1VendorName := TStringField.Create(Self);
Table1VendorName.FieldName := ´VendorName´;
Table1VendorName.DataSet := Table1;
Table1VendorName.LookupDataSet := Table2;
Table1VendorName.LookupKeyFields := ´VendorNo´;
Table1VendorName.LookupResultField := ´VendorName´;
Table1VendorName.KeyFields := ´VendorNo´;
Table1VendorName.Size := 30;
Table1VendorName.Lookup := True;
Table2.Open;
Table1.Open;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Table1.Close;
Table2.Close;
FreeAndNil(Table1VendorName);
end;
end.object Form1: TForm1 Left = 200 Top = 108 Width = 544 Height = 375 Caption = ´Form1´ Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = ´MS Sans Serif´ Font.Style = [] OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object DBGrid1: TDBGrid Left = 8 Top = 64 Width = 521 Height = 249 DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = ´MS Sans Serif´ TitleFont.Style = [] end object DBNavigator1: TDBNavigator Left = 184 Top = 32 Width = 240 Height = 25 DataSource = DataSource1 TabOrder = 1 end object Table1: TTable DatabaseName = ´DBDemos´ SessionName = ´Default´ TableName = ´parts.db´ Left = 56 Top = 24 object Table1PartNo: TFloatField FieldName = ´PartNo´ end object Table1VendorNo: TFloatField FieldName = ´VendorNo´ end end object DataSource1: TDataSource DataSet = Table1 Left = 88 Top = 24 end object Table2: TTable DatabaseName = ´DBDemos´ SessionName = ´Default´ TableName = ´vendors.db´ Left = 128 Top = 24 object Table2VendorNo: TFloatField FieldName = ´VendorNo´ end object Table2VendorName: TStringField FieldName = ´VendorName´ Size = 30 end object Table2Address1: TStringField FieldName = ´Address1´ Size = 30 end object Table2Address2: TStringField FieldName = ´Address2´ Size = 30 end object Table2City: TStringField FieldName = ´City´ end object Table2State: TStringField FieldName = ´State´ end object Table2Zip: TStringField FieldName = ´Zip´ Size = 10 end object Table2Country: TStringField FieldName = ´Country´ Size = 15 end object Table2Phone: TStringField FieldName = ´Phone´ Size = 15 end object Table2FAX: TStringField FieldName = ´FAX´ Size = 15 end object Table2Preferred: TBooleanField FieldName = ´Preferred´ end end end
GOSTEI 0
Crpavao
03/06/2004
Fiz tudo isto que vcs estão dizendo mas este novo campo não aparece na lista dos Campos da tabela.
Fiz uma showmessage dos campos da tabela e este não aparece.
Minhas 2 queries são criadas em tempo de execuçao.
Daí eu tento criar um novo campo tipo ftlookup numa das queries e este novo campo não aparece:
vF := vQuery.Fields[vI].FieldName;
vQuery.Close;
vNew := TStringField.Create(vQuery);
vNew.DisplayLabel := QyCampo1Es_Campo.Value + ´**´;
vNew.FieldKind := fkLookup;
vNew.FieldName := ´lK´ + vF;
vNew.LookupDataSet := TADOQuery(FindComponent(´Q´ + vTable + uMenu.Menu.Strzero(vLook,2)));
vNew.LookupKeyFields := ´Cd_Opcao´;
vNew.LookupResultField := ´Ds_Opcao´;
vNew.KeyFields := vF;
vNew.Size := 15;
vNew.Lookup := True;
vNew.Visible := True;
vQuery.FieldDefList.Update;
vQuery.Open;
Alguém me socorre?
Fiz uma showmessage dos campos da tabela e este não aparece.
Minhas 2 queries são criadas em tempo de execuçao.
Daí eu tento criar um novo campo tipo ftlookup numa das queries e este novo campo não aparece:
vF := vQuery.Fields[vI].FieldName;
vQuery.Close;
vNew := TStringField.Create(vQuery);
vNew.DisplayLabel := QyCampo1Es_Campo.Value + ´**´;
vNew.FieldKind := fkLookup;
vNew.FieldName := ´lK´ + vF;
vNew.LookupDataSet := TADOQuery(FindComponent(´Q´ + vTable + uMenu.Menu.Strzero(vLook,2)));
vNew.LookupKeyFields := ´Cd_Opcao´;
vNew.LookupResultField := ´Ds_Opcao´;
vNew.KeyFields := vF;
vNew.Size := 15;
vNew.Lookup := True;
vNew.Visible := True;
vQuery.FieldDefList.Update;
vQuery.Open;
Alguém me socorre?
GOSTEI 0