Fórum Comparação de chaves de uma tabela utilizando count #192755
04/11/2003
0
procedure Tfrmcompesc.btconfirmarClick(Sender: TObject);
begin
fim;
if application.messagebox(´Confirma a Atualização?´,´Confirme´, mb_yesno) = idyes then
begin
try
if strtoint(dbcod.text) <> tbgiroCod_prod.asinteger then
begin
with tbesc do
begin
post;
refresh;
end;
with tbgiro do
begin
insert;
tbgiroCod_prod.asinteger := strtoint(dbcod.text);
tbgiroNome.asstring := dbnome.text;
tbgiroModelo.asstring := dbmodel.text;
tbgiroQt_ent.asinteger := strtoint(dbqtde.text);
tbgiroQt_sai.asinteger := 0;
tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger;
tbgiro.post;
tbgiro.refresh;
end
end
else
if strtoint(dbcod.text) = tbgiroCod_prod.asinteger then
begin
with tbesc do
begin
post;
refresh;
end;
with tbgiro do
begin
edit;
tbgiroQt_ent.asinteger := tbgiroQt_ent.asinteger + strtoint(dbqtde.text);
tbgiroQt_sai.asinteger := tbgiroQt_sai.asinteger;
tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger - tbgiroQt_sai.asinteger;
post;
refresh;
end;
end;
end;
except begin
MessageDlg (´Não foi possível registrar esta compra´, mtInformation,[mbOk],0);
exit;
end;
end;
end;
end;
Elias Jr
Curtir tópico
+ 0Posts
04/11/2003
Brnz
BRNz...
Gostei + 0
04/11/2003
Elias Jr
Olá amigo, olha com o código dessa forma ele consegue executar o programa mas apenas para o primeiro produto cadastrado(tipo, caneta). A partir daí eu posso cadastrar mais canetas que ele vai funcionar e me dar a quantidade comprada sem problema. Mas ao inserir um outro produto ele acusa keyviolation. O que eu quero é o seguinte: verificar se o código do produto cadastrado é igual a algum código existente na tabela. Se for, ele cadastra e soma o total do produto mais a quantidade comprada recentemente. Se não ele cadastra o novo produto e o total desse produto recebe a quantidade comprada. Só que eu estou errando pra utilizar tipo um count pra ele verificar a tabela chave por chave.
Gostei + 0
04/11/2003
Brnz
procedure Tfrmcompesc.btconfirmarClick(Sender: TObject);
begin
| fim;
| if application.messagebox(´Confirma a Atualização?´,´Confirme´, mb_yesno) = idyes then
| begin
| | try
| | if strtoint(dbcod.text) <> tbgiroCod_prod.asinteger then
| | begin
| | | with tbesc do
| | | begin
| | | | post;
| | | | refresh;
| | | end;
| | | with tbgiro do
| | | begin
| | | | insert;
| | | | tbgiroCod_prod.asinteger := strtoint(dbcod.text);
| | | | tbgiroNome.asstring := dbnome.text;
| | | | tbgiroModelo.asstring := dbmodel.text;
| | | | tbgiroQt_ent.asinteger := strtoint(dbqtde.text);
| | | | tbgiroQt_sai.asinteger := 0;
| | | | tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger;
| | | | tbgiro.post;
| | | | tbgiro.refresh;
| | | end { <== Sem Ponto-e-Virgula??? }
| | end
| | else
| | if strtoint(dbcod.text) = tbgiroCod_prod.asinteger then
| | begin
| | | with tbesc do
| | | begin
| | | | post;
| | | | refresh;
| | | end;
| | | with tbgiro do
| | | begin
| | | | edit;
| | | | tbgiroQt_ent.asinteger := tbgiroQt_ent.asinteger + strtoint(dbqtde.text);
| | | | tbgiroQt_sai.asinteger := tbgiroQt_sai.asinteger;
| | | | tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger - tbgiroQt_sai.asinteger;
| | | | post;
| | | | refresh;
| | | end;
| | end;
| |
| | ?????
| | |
| | end;
| | except
| | begin
| | | MessageDlg (´Não foi possível registrar esta compra´, mtInformation,[mbOk],0);
| | | exit;
| | end;
| end;
end;
????
|
end;Viu?!?
BRNz...
Gostei + 0
04/11/2003
Elias Jr
Gostei + 0
04/11/2003
Elias Jr
Gostei + 0
05/11/2003
Brnz
se for igual... altera...
procedure Tfrmcompesc.btconfirmarClick(Sender: TObject); var i:integer; begin fim; if application.messagebox(´Confirma a Atualização?´,´Confirme´, mb_yesno) = idyes then begin try begin tbgiro.first; for i:=0 to tbgiro.recordcount do begin if strtoint(dbcod.text) <> tbgiroCod_prod.asinteger then begin with tbesc do begin post; refresh; end; with tbgiro do begin insert; tbgiroCod_prod.asinteger := strtoint(dbcod.text); tbgiroNome.asstring := dbnome.text; tbgiroModelo.asstring := dbmodel.text; tbgiroQt_ent.asinteger := strtoint(dbqtde.text); tbgiroQt_sai.asinteger := 0; tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger; tbgiro.post; tbgiro.refresh; end; end else if strtoint(dbcod.text) = tbgiroCod_prod.asinteger then begin with tbesc do begin post; refresh; end; with tbgiro do begin edit; tbgiroQt_ent.asinteger := tbgiroQt_ent.asinteger + strtoint(dbqtde.text); tbgiroQt_sai.asinteger := tbgiroQt_sai.asinteger; tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger - tbgiroQt_sai.asinteger; post; refresh; end; end; tbgiro.next; end; except begin MessageDlg (´Não foi possível registrar esta compra´, mtInformation,[mbOk],0); exit; end; end; end;
Agora... ele vai fazer isso alterando ou inserindo para cada registro da tabela...
ou seja... se a tabela tiver 50 registros sendo q nenhum eh igual ao código informado, serão inseridos 50 registros com o novo código... e... eeeeeeeh... vai dar KeyViolation...
baum... dexa eu refazer o código... pq o principio do que vc quer eh diferente... tenq fazer a consulta... e depois incluir... jah mando... peraue!
BRNz...
Gostei + 0
05/11/2003
Brnz
procedure Tfrmcompesc.btconfirmarClick(Sender: TObject);
begin
fim;
if application.messagebox(´Confirma a Atualização?´,´Confirme´, mb_yesno) = idyes then
begin
try
begin
tbgiro.indexname:=´´;
if tbgiro.findkey([strtoint(dbcod.text)]) then {Verifica se o dado contido em dbcod.text existe em qualquer registro da tabela no campo da Chave Primária}
begin
with tbesc do
begin
post;
refresh;
end;
with tbgiro do
begin
insert;
tbgiroCod_prod.asinteger := strtoint(dbcod.text);
tbgiroNome.asstring := dbnome.text;
tbgiroModelo.asstring := dbmodel.text;
tbgiroQt_ent.asinteger := strtoint(dbqtde.text);
tbgiroQt_sai.asinteger := 0;
tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger;
tbgiro.post;
tbgiro.refresh;
end;
end
else
begin
with tbesc do
begin
post;
refresh;
end;
with tbgiro do
begin
edit;
tbgiroQt_ent.asinteger := tbgiroQt_ent.asinteger + strtoint(dbqtde.text);
tbgiroQt_sai.asinteger := tbgiroQt_sai.asinteger;
tbgiroQt_tot.asinteger := tbgiroQt_ent.asinteger - tbgiroQt_sai.asinteger;
post;
refresh;
end;
end;
end;
except
begin
MessageDlg (´Não foi possível registrar esta compra´, mtInformation,[mbOk],0);
exit;
end;
end;
end;Tentaie.... achu q funfa...
BRNz...
Gostei + 0
05/11/2003
Elias Jr
testei o seu código mas infelizmente ele está acusando keyviolation novamente...é isso mesmo que eu quero...que ele verifique na tabela, se não existir ele faz o procedimento de cima, caso exista ele fará o outro procedimento. Como será que resolvo isso? Já tentei utilizar o RecordCount mas não deu. Acho que errei em algo...
Gostei + 0
05/11/2003
Brnz
tipo... poe um not no if do findkey.... ou seja...
...
begin
try
begin
tbgiro.indexname:=´´;
{-->} if NOT tbgiro.findkey([strtoint(dbcod.text)]) then
begin
.......heuheu...
BRNz...
Gostei + 0
05/11/2003
Elias Jr
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)