Home > Back-end >  Adodataset how to dynamically add lookup fields
Adodataset how to dynamically add lookup fields

Time:10-20

I have two tables, a stock of the archive table, a schedule of documents, documents, records only the stock code in the list, when want to increase your documents showing name of [stock], [specification], [unit] and other relevant information, the need to manage inventory file table, I now use adodataset connect to the database, but always prompt when increase lookup column "... Commodity name 'field' always be a calculated or lookup field ", not just where is not correct, the following is the main code, please help those who have had previous experience,
.
.
Splookupdataset:=Tadodataset. Create (nil);
Splookupdataset.Com mandText:='select bm, MC, gg from t_bm_sp';
Splookupdataset. Connection:=dm1. DmADOCon1;
Splookupdataset. Open;
Adodsdetail.Com mandType:=cmdtext;
Adodsdetail.Com mandText:='select * from mx_acceptAffirm_c';
Adodsdetail. Connection:=dm1. DmADOCon1;
AddCalField (adodsdetail splookupdataset, 'MC' fkLookup, 'name of commodity).
.
.
The function AddCalField (dataset, LookupDataSet: TADOdataset; CalFieldName: String; CalFieldKind: TFieldKind; CalFieldCaption: string) : Boolean;
Var fldLookup: TField;
The begin
Result:=false;
If the Dataset. FindField (CalFieldName) & lt;> Nil then
exit;
If the Dataset. The Active then the Dataset. The Close;
FldLookup:=TField. Create (dataset);
FldLookup. ReadOnly:=true;
FldLookup. LookupCache:=true;
FldLookup. Lookup:=true;
FldLookup. FieldName:=CalFieldName;
FldLookup. DisplayLabel:=CalFieldCaption;
FldLookup. Name:=dataset. The Name + fldLookup. FieldName;
FldLookup. The DataSet:=the DataSet;
FldLookup. FieldKind:=CalFieldKind;
FldLookup. LookupDataSet:=LookupDataSet;
FldLookup. LookupKeyFields:='bm';
FldLookup. KeyFields:='Bm_sp';
FldLookup. LookupResultField:=CalFieldName;
The dataset. FieldDefs. Update;//to prompt the error
Result:=true;
end;
  • Related