Erro ao Deletar na Tabela
a rotina seguinte descreve a baixa de livros de um sistema para biblioteca, onde os dados que estou trabalhando na tabela ´Emprestimo´, sao transferidos para a tabela ´baixa´, e logo em seguida são deletados os mesmos dados da tabela ´emprestimo´. porem o sistema acusa um erro que nao estou conseguindo ver. O erro é referente a quando deleto os dados da tabela emprestimo. segue abaixo o erro e o codigo:
erro:
---------------------------------------------------------------
´Tbl_Emprestimo: DataSet Not in Edit or Insert Mode´
---------------------------------------------------------------
Codigo:
---------------------------------------------------------------
procedure TEmprestimo.Btn_BaixarClick(Sender: TObject);
var
d1, d2: TDateTime;
vdDATA, multa: extended;
Cod1, Cod2, viMINUTOS, viHORAS,zera: Integer;
begin
if Combo_TipoConsulta.Text <> ´´ then begin
Dm.Tbl_TipoConsulta.Close;
Dm.Tbl_TipoConsulta.SelectSQL.Clear;
Dm.Tbl_TipoConsulta.SelectSQL.Add(´select * from TIPO_CONSULTA Where TIPO = ´´´ + Trim(Combo_TipoConsulta.Text) + ´´´´);
Dm.Tbl_TipoConsulta.Open;
end;
Edit_DataBaixa.Text := FormatDateTime(´dd/mm/yyyy´, Date);
vdDATA := DaysBetween(Date, StrToDateTime(Edit_DATAEMPR.Text));
//----------------------------------------------------------------------------
if Dm.Tbl_TipoConsultaUNIDADE.AsString = ´Dia(s)´ then begin
multa := ((((vdDATA / StrToFloat(DM.Tbl_TipoConsultaTEMPO.AsString) - 1)) * StrToFloat(DM.Tbl_TipoConsultaMULTA.AsString)));
d1 := StrToDate(Edit_DataBaixa.Text);
d2 := StrToDate(Edit_DataDev.Text);
if Combo_Tipo.Text = ´ALUNO´ then begin
if d1 > d2 then
begin
Zera := Application.MessageBox(´Realmente deseja dar baixa?´, ´CONFIRME´, mb_yesno);
if zera = IDYES Then
begin
//ShowMessage(´emprestimo para aluno com multa:´ + FloatToStr(multa))
// Contador de codigo
Dm.Tbl_baixa.Last;
Cod1 := Dm.Tbl_baixaCodigo.AsInteger;
Cod2 := Cod1 + 1;
//--------------------------------------
If Not(DM.Tr_Baixa.InTransaction) Then
DM.Tr_Baixa.StartTransaction;
Dm.Tbl_baixa.insert;
Edit_Multa.Text := Floattostr(multa);
Dm.Tbl_baixaCODIGO.asInteger := Cod2;
Dm.Tbl_baixaCOD_ALUNO.AsInteger := StrToInt(Edit_CodigoAl.Text);
Dm.Tbl_baixaCOD_LIVRO.AsInteger := StrToInt(Edit_CodigoLivro.Text);
Dm.Tbl_baixaTIPO_EMPR.AsString := Combo_Tipo.Text;
Dm.Tbl_baixaTIPO_CONS.AsString := Combo_TipoConsulta.Text;
Dm.Tbl_baixaDATA_EMPR.AsDateTime := StrToDateTime(Edit_DataEmpr.Text);
Dm.Tbl_baixaDATA_DEV.AsDateTime := StrToDateTime(Edit_DataDev.Text);
Dm.Tbl_baixaDATA_BAIXA.AsDateTime := StrToDateTime(Edit_DataBaixa.Text);
Dm.Tbl_baixaMULTA.AsString := Edit_Multa.Text;
Dm.Tbl_baixa.post;
Dm.Tbl_baixa.applyupdates;
// deleta da tabela o emprestimo -> AQUI ESTÁ O ERRO!
if Edit_codigo.Text <>´´ Then
begin
Dm.Tbl_Emprestimos.Close;
Dm.Tbl_Emprestimos.SelectSQL.Clear;
Dm.Tbl_Emprestimos.SelectSQL.Add(´select * From Emprestimo Where CODIGO = ´´´ + Trim(Edit_codigo.Text) + ´´´´);
Dm.Tbl_Emprestimos.Open;
DM.Tbl_Emprestimos.Delete;
Dm.Tbl_Emprestimos.post;
Dm.Tbl_Emprestimos.applyupdates;
end;
end;
end;
end;
end;
end;
end;
erro:
---------------------------------------------------------------
´Tbl_Emprestimo: DataSet Not in Edit or Insert Mode´
---------------------------------------------------------------
Codigo:
---------------------------------------------------------------
procedure TEmprestimo.Btn_BaixarClick(Sender: TObject);
var
d1, d2: TDateTime;
vdDATA, multa: extended;
Cod1, Cod2, viMINUTOS, viHORAS,zera: Integer;
begin
if Combo_TipoConsulta.Text <> ´´ then begin
Dm.Tbl_TipoConsulta.Close;
Dm.Tbl_TipoConsulta.SelectSQL.Clear;
Dm.Tbl_TipoConsulta.SelectSQL.Add(´select * from TIPO_CONSULTA Where TIPO = ´´´ + Trim(Combo_TipoConsulta.Text) + ´´´´);
Dm.Tbl_TipoConsulta.Open;
end;
Edit_DataBaixa.Text := FormatDateTime(´dd/mm/yyyy´, Date);
vdDATA := DaysBetween(Date, StrToDateTime(Edit_DATAEMPR.Text));
//----------------------------------------------------------------------------
if Dm.Tbl_TipoConsultaUNIDADE.AsString = ´Dia(s)´ then begin
multa := ((((vdDATA / StrToFloat(DM.Tbl_TipoConsultaTEMPO.AsString) - 1)) * StrToFloat(DM.Tbl_TipoConsultaMULTA.AsString)));
d1 := StrToDate(Edit_DataBaixa.Text);
d2 := StrToDate(Edit_DataDev.Text);
if Combo_Tipo.Text = ´ALUNO´ then begin
if d1 > d2 then
begin
Zera := Application.MessageBox(´Realmente deseja dar baixa?´, ´CONFIRME´, mb_yesno);
if zera = IDYES Then
begin
//ShowMessage(´emprestimo para aluno com multa:´ + FloatToStr(multa))
// Contador de codigo
Dm.Tbl_baixa.Last;
Cod1 := Dm.Tbl_baixaCodigo.AsInteger;
Cod2 := Cod1 + 1;
//--------------------------------------
If Not(DM.Tr_Baixa.InTransaction) Then
DM.Tr_Baixa.StartTransaction;
Dm.Tbl_baixa.insert;
Edit_Multa.Text := Floattostr(multa);
Dm.Tbl_baixaCODIGO.asInteger := Cod2;
Dm.Tbl_baixaCOD_ALUNO.AsInteger := StrToInt(Edit_CodigoAl.Text);
Dm.Tbl_baixaCOD_LIVRO.AsInteger := StrToInt(Edit_CodigoLivro.Text);
Dm.Tbl_baixaTIPO_EMPR.AsString := Combo_Tipo.Text;
Dm.Tbl_baixaTIPO_CONS.AsString := Combo_TipoConsulta.Text;
Dm.Tbl_baixaDATA_EMPR.AsDateTime := StrToDateTime(Edit_DataEmpr.Text);
Dm.Tbl_baixaDATA_DEV.AsDateTime := StrToDateTime(Edit_DataDev.Text);
Dm.Tbl_baixaDATA_BAIXA.AsDateTime := StrToDateTime(Edit_DataBaixa.Text);
Dm.Tbl_baixaMULTA.AsString := Edit_Multa.Text;
Dm.Tbl_baixa.post;
Dm.Tbl_baixa.applyupdates;
// deleta da tabela o emprestimo -> AQUI ESTÁ O ERRO!
if Edit_codigo.Text <>´´ Then
begin
Dm.Tbl_Emprestimos.Close;
Dm.Tbl_Emprestimos.SelectSQL.Clear;
Dm.Tbl_Emprestimos.SelectSQL.Add(´select * From Emprestimo Where CODIGO = ´´´ + Trim(Edit_codigo.Text) + ´´´´);
Dm.Tbl_Emprestimos.Open;
DM.Tbl_Emprestimos.Delete;
Dm.Tbl_Emprestimos.post;
Dm.Tbl_Emprestimos.applyupdates;
end;
end;
end;
end;
end;
end;
end;
Mahdak
Curtidas 0
Respostas
Rjun
24/05/2005
Em vez de você fazer um select para depois excluir o registro, você poderia fazer assim :
if Edit_codigo.Text <>´´ then begin Dm.Tbl_Emprestimos.Close; Dm.Tbl_Emprestimos.SelectSQL.Clear; Dm.Tbl_Emprestimos.SelectSQL.Add(´Delete From Emprestimo Where CODIGO = ´´´ + Trim(Edit_codigo.Text) + ´´´´); Dm.Tbl_Emprestimos.ExecSql; end;
GOSTEI 0
Mahdak
24/05/2005
Em vez de você fazer um select para depois excluir o registro, você poderia fazer assim :
if Edit_codigo.Text <>´´ then begin Dm.Tbl_Emprestimos.Close; Dm.Tbl_Emprestimos.SelectSQL.Clear; Dm.Tbl_Emprestimos.SelectSQL.Add(´Delete From Emprestimo Where CODIGO = ´´´ + Trim(Edit_codigo.Text) + ´´´´); Dm.Tbl_Emprestimos.ExecSql; end;
Valei Irmão era isso mesmo... problema resolvido, mas agora pintou outro erro, mas em outro codigo. O erro é: ´Tbl_Livros: Field CODIGO not Found´
ja verifiquei a tabela, a estrutura está certa, o campo CODIGO existe, em fim, está tudo aparentemente correto. vou colar o código abaixo pra ver se vc sabe me dizer se existe algo errado nele:
// Verifica o nome do livro
if Edit_CodigoLivro.Text <>´´ Then
begin
Dm.Tbl_Livros.Close;
Dm.Tbl_Livros.SelectSQL.Clear;
Dm.Tbl_Livros.SelectSQL.Add(´select NOME from LIVROS Where CODIGO = ´+ Trim(Edit_codigoLivro.Text));
//Dm.Tbl_Livros.SelectSQL.Add(´select NOME from LIVROS Where CODIGO = ´´´ + Trim(Edit_CodigoLivro.Text) + ´´´´);
Dm.Tbl_Livros.Open;
end;
GOSTEI 0