2006/02/26 | LookUP 字段
类别(语言类学习笔记) | 评论(0) | 阅读(162) | 发表于 15:31
标题: LookUP 字段 <BR />
用户: wwle  时间: 2005-4-13 1:37:11 ID: 757 <BR />
<BR />
//创建固定字段 <BR />
procedure addYJField(QryR:TadoQuery); <BR />
var <BR />
temQry:TadoQuery; <BR />
i:integer; <BR />
begin <BR />
temQry:=TadoQuery.Create(nil); <BR />
temQry.connection:= dmCmn_DataModule.DsrConnect; <BR />
try <BR />
temQry.Clone(qryr,ltReadOnly); <BR />
qryR.Close; <BR />
qryR.Fields.Clear; <BR />
for i := 0 to temQry.FieldDefs.Count - 1 do <BR />
begin <BR />
with temQry.FieldDefs.Items[i].FieldClass.Create(nil) do <BR />
begin <BR />
FieldName := temQry.FieldDefs.Items[i].name; <BR />
size := temQry.FieldDefs.Items[i].size; <BR />
Index := i; <BR />
DataSet := qryR; <BR />
end; <BR />
end; <BR />
qryR.FieldDefs.UpDate; <BR />
finally <BR />
qryR.open; <BR />
temQry.Free; <BR />
end; <BR />
end; <BR />
<BR />
// 自动添加LookUp类型字短 <BR />
procedure AddLookUpFieldFy(QRYL,QryR:TadoQuery;FieldName:string;DisplayLabel:string;KeyFields:string;LookFields:string;LookResultField:string); <BR />
var <BR />
t: TStringField; <BR />
begin <BR />
if QrYR.Fields.FindField(FieldName)<>nil then Exit; <BR />
qryR.Close; <BR />
t := TStringField.Create(QryR); <BR />
t.FieldKind := fkLookup; <BR />
t.FieldName := FieldName; <BR />
t.DisplayLabel:=DisplayLabel; <BR />
t.Size := 50; <BR />
t.LookupDataSet := QryL; <BR />
t.KeyFields := KeyFields;//'费用来源;itemCode'; <BR />
t.LookupKeyFields := LookFields;//'费用来源;itemCode'; <BR />
t.LookupResultField :=LookResultField;//'费用名称'; <BR />
t.DataSet := QryR; <BR />
qryR.Open; <BR />
end; <BR />
<BR />
<BR />
<BR />
procedure TForm2.Button1Click(Sender: TObject); <BR />
var <BR />
i:integer; <BR />
begin <BR />
self.Qry.Open; <BR />
With Self.Qry do <BR />
begin <BR />
TDataSetDesigner.Create(Self.Qry); <BR />
Try <BR />
Designer.BeginDesign; <BR />
For i:=0 to FieldDefList.Count-1 do <BR />
begin <BR />
FieldDefList.FieldDefs[i].CreateField(Self); <BR />
//保留原有字段 <BR />
end; <BR />
//新增一个字段 <BR />
With DB.DefaultFieldClasses[ftString].Create(Self) do <BR />
begin <BR />
FieldKind:=fkLookup; <BR />
FieldName:='oo'; <BR />
LookupDataSet := ADOQuery1; <BR />
KeyFields := 'itmCode';//'费用来源;itemCode'; <BR />
LookupKeyFields := 'itmCode';//'费用来源;itemCode'; <BR />
LookupResultField :='品目名称';//'费用名称'; <BR />
DataSet:=Self.Qry; <BR />
end; <BR />
Finally <BR />
Designer.EndDesign; <BR />
Designer.Free; <BR />
end; <BR />
end; <BR />
end; <BR />
<BR />
新方法 添加 字段 哦 <BR />
<BR />
//////////////////添加一个固定字段 <BR />
<BR />
With Self.ADOQuery1 do <BR />
begin <BR />
TDataSetDesigner.Create(Self.ADOQuery1); <BR />
Try <BR />
Designer.BeginDesign; <BR />
For i:=0 to FieldDefList.Count-1 do <BR />
begin <BR />
FieldDefList.FieldDefs[i].CreateField(Self); <BR />
//保留原有字段 <BR />
end; <BR />
//新增一个字段 <BR />
With DB.DefaultFieldClasses[TFieldType(2)].Create(Self) do <BR />
begin <BR />
FieldKind:=TFieldKind(1); <BR />
FieldName:='FieldName1'; <BR />
Name:='FieldName1'; <BR />
DataSet:=Self.ADOQuery1; <BR />
end; <BR />
Finally <BR />
Designer.EndDesign; <BR />
Designer.Free; <BR />
end; <BR />
end;



procedure TForm1.Button1Click(Sender: TObject);
var
T: TStringField;
begin
self.ADOTable1.Close;
T := TStringField.Create(Self);
T.FieldName := 'myfield';
T.Name := self.ADOTable1.Name + T.FieldName;
T.Index := self.ADOTable1.FieldCount;
T.DataSet := self.ADOTable1;
T.FieldKind:=fkCalculated;
self.ADOTable1.Open;
end;

procedure TForm1.ADOTable1CalcFields(DataSet: TDataSet);
begin
if DataSet.RecNo = -1 then
DataSet.Fieldvalues['myField'] := 1
else
DataSet.Fieldvalues['myField'] := DataSet.RecNo;
end;
0

评论Comments