In my Delphi application, I use lookup fields, but in unusual way. Actually, I wanna update field in underlying data set, just like if it was in the same table.
Existing guides tell that there is no problem, just join the table and voila... I envy if they really succeeded this task with such simple solution. I do not. BTW I think I'm getting close to reach my goal. I have one question left: how the hell I can get value I just entered into DBGrid Cell?
I tried DBGrid[FieldName].EditValue
and .DisplayText
, but they show the same value as Field.Value
, which doesn't change after exiting the column, because it is lookup field. Sender.NewValue
is null. I'm using this function to update lookup table:
procedure TKDGridForm.LookupFieldChange(Sender: TField);
begin
if not Assigned(Sender) then
Exit;
Sender.OnChange := nil;
if not Assigned(Sender.LookupDataSet) then
Exit;
if Sender.LookupDataSet.Locate(Sender.LookupKeyFields, Sender.DataSet[Sender.KeyFields], []) then
Sender.LookupDataSet.Edit
else
Sender.LookupDataSet.Append;
// how do I get the value I just entered?
Sender.Value := KDGrid3[Sender.FieldName].DisplayText;
Sender.LookupDataSet.FieldValues[Sender.LookupResultField] := Sender.Value;
Sender.LookupDataSet.Post;
Sender.OnChange := LookupFieldChange;
end;
Here is SQL I used before I ended up with lookup fields:
select det.*,
od1.T_EQ T_SHABLON_EQ,
od1.T_NV T_SHABLON_NV,
od1.T_PRIM T_SHABLON_PRIM,
od2.T_EQ T_PRAVKA_EQ,
od2.T_NV T_PRAVKA_NV,
od2.T_PRIM T_PRAVKA_PRIM,
od3.T_EQ T_VALCOV_EQ,
od3.T_NV T_VALCOV_NV,
od3.T_PRIM T_VALCOV_PRIM,
od4.T_EQ T_REZKA2_EQ,
od4.T_NV T_REZKA2_NV,
od4.T_PRIM T_REZKA2_PRIM
from CMKNEW.details det
left join CMKNEW.OperDetails od1
ON det.nrec = od1.cdetail
and 81 = od1.coper
left join CMKNEW.OperDetails od2
ON det.nrec = od2.cdetail
and 82 = od2.coper
left join CMKNEW.OperDetails od3
ON det.nrec = od3.cdetail
and 83 = od3.coper
left join CMKNEW.OperDetails od4
ON det.nrec = od4.cdetail
and 84 = od4.coper
where det.ckd=:CKD order by det.NREC
Hope it will explain my task clearer. If you wanna mcve, I can extend this, though I think it's not essential.
My database is Oracle, connected through ADO. I'd like the solution to be as simple as possible.