Fórum Muito trivial mas....... #145113
13/03/2003
0
Essa transação é muito trivial, mas esta me matando de raíva, utilizando o componente table, atraves de um botao ponho a tabela em mode de adição e apos chamo a função achou para comparar se ja existe uma tupla com o código digitado pelo usuario, utilizando os procedimentos seguintes aparentemente funcionam, mas ainda qando o codigo ja existe me é retornado o mensagem KEY VIOLATION, como podem ver ainda nao domino a ferramenta como vcs.
function achou(cod : string):boolean;
begin
achou := false;
DMadm.Tbempresa.Open;
DMadm.Tbempresa.First;
while not DMadm.Tbempresa.Eof do
begin
if DMadm.Tbempresa.FieldByName(´CodEmp´).AsString = cod then
achou := true;
DMadm.Tbempresa.Next;
end;
end;
procedure TFrmEmpresa.DBEdit1Exit(Sender: TObject);
begin
if DMadm.DSempresa.State in [dsEdit, dsInsert] then
begin
if achou(DBEdit1.text) = true then
begin
MessageDlg(´O código ´+DBEdit1.text+´ já existe.´,
mtInformation,[mbOk], 0);
DBEdit1.Clear;
DBEdit1.SetFocus;
end;
end;
end;
procedure TFrmEmpresa.BtnincluirClick(Sender: TObject);
begin
try
DMadm.Tbempresa.Last;
LimpaForm;
Btnincluir.Caption := ´&Confirma´;
if Btnincluir.Caption = ´&Confirma´ then
begin
DMadm.Tbempresa.Insert;
DBEdit1.SetFocus;
end;
Btnincluir.Caption := ´&Incluir´
except
end;
end;
:cry:
function achou(cod : string):boolean;
begin
achou := false;
DMadm.Tbempresa.Open;
DMadm.Tbempresa.First;
while not DMadm.Tbempresa.Eof do
begin
if DMadm.Tbempresa.FieldByName(´CodEmp´).AsString = cod then
achou := true;
DMadm.Tbempresa.Next;
end;
end;
procedure TFrmEmpresa.DBEdit1Exit(Sender: TObject);
begin
if DMadm.DSempresa.State in [dsEdit, dsInsert] then
begin
if achou(DBEdit1.text) = true then
begin
MessageDlg(´O código ´+DBEdit1.text+´ já existe.´,
mtInformation,[mbOk], 0);
DBEdit1.Clear;
DBEdit1.SetFocus;
end;
end;
end;
procedure TFrmEmpresa.BtnincluirClick(Sender: TObject);
begin
try
DMadm.Tbempresa.Last;
LimpaForm;
Btnincluir.Caption := ´&Confirma´;
if Btnincluir.Caption = ´&Confirma´ then
begin
DMadm.Tbempresa.Insert;
DBEdit1.SetFocus;
end;
Btnincluir.Caption := ´&Incluir´
except
end;
end;
:cry:
Anonymous
Curtir tópico
+ 0
Responder
Posts
13/03/2003
E_gama
Experimente fazer essa modificacao (coloquei as alteracoes em maiusculas):
function achou(cod : string):boolean; begin RESULT := FALSE; DMadm.Tbempresa.Open; DMadm.Tbempresa.First; while (not DMadm.Tbempresa.Eof) OR (RESULT = TRUE) do begin if DMadm.Tbempresa.FieldByName(´CodEmp´).AsString = cod then RESULT := TRUE; DMadm.Tbempresa.Next; end; end;
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)