const NO_BUSCAR = 1;
BUSCAR = 0;
BUSCAR_SI_CAMBIA = 2; procedure AutoCompleta( Ctr:TcustomEdit; qry:TDataset;const NombreCampo:string); overload;
procedure AutoCompleta( Ctr:TComboBox; qry:TDataset;const NombreCampo:string);overload;
procedure AutoCompleta(Ctr:TcustomEdit; qry:TDataset;const NombreCampo:string);
const checking :Boolean = False;
OldText:string='';
var Campo:TField;
begin
case key of
vk_DELETE,VK_BACK :
begin
Ctr.SelText := '';
Ctr.Tag:= codigoutil.IfThen(Ctr.Tag=BUSCAR_SI_CAMBIA,BUSCAR, NO_BUSCAR);
end;
VK_LEFT,VK_CAPITAL, VK_RIGHT, VK_HOME, VK_END: Ctr.Tag:= NO_BUSCAR;
else
Ctr.Tag := codigoutil.IfThen(Ctr.Tag=BUSCAR_SI_CAMBIA,NO_BUSCAR, BUSCAR);
end;
if Ctr.Tag = BUSCAR then
begin
if checking then Exit;
checking:= True;
if (Ctr.SelStart = 0) then
OldText := Ctr.Text
else
OldText:= Copy(Ctr.Text,1,Ctr.SelStart);if not qry.Active then
qry.Open;
if qry.Locate(NombreCampo, OldText ,[locaseinsensitive, lopartialkey]) then
begin
Campo :=qry.FindField(NombreCampo);
if Campo = nil then
Exception.Create('proc AutoCompleta: Campo '+QuotedStr(NombreCampo)+' no encontrado')
else
begin
if Key = vk_return then
Ctr.Text:= Campo.AsString
else
Ctr.Text:= OldText + Rightstr(Campo.AsString,Length(Campo.AsString)-Length(OldText));
Ctr.SelStart:= Length(OldText);
Ctr.SelLength := Length(Ctr.Text);
Ctr.Tag:= BUSCAR;
end;
end
else
begin
Ctr.Tag:= BUSCAR_SI_CAMBIA;
end;
checking:= False;
end;
end;