Saudações Alunos e programadores,
abaixo segue um conjunto de rotinas utilizadas no processo de atualização de uma Tupla, escolhida pelo usuário, para ser atualizada por meio do comando em SQL denominado "UPDATE".
Uma série de verificações é realizada antes da atualização ser realizada com sucesso.
Observe com o seguinte espírito: 
"... Você pode fazer diferente, Você pode fazer melhor ..."
Um grande abraço
Professor Alexandre Manoel
//==============================================================================
function  Talx_frm_sgc_contas_bancarias.alx_boo_Conta_Bancaria_Alterada_Com_Sucesso(): Boolean;
var
  alx_str_sql_text    : TStrings;
  alx_boo_apoio       : Boolean;
  alx_int_cb_id       : Integer;
  alx_str_cb_nome     : String;
  alx_str_cb_codigo   : String;
  alx_int_cb_ab_id    : Integer;
  alx_int_cb_un_id    : Integer;
begin
  try
    alx_int_cb_id     := strtoint(self.Edit2.Text);
    alx_str_cb_nome   := self.Edit3.Text;
    alx_str_cb_codigo := self.Edit4.Text;
    alx_int_cb_ab_id  := strtoint(self.edit5.Text);
    //--------------
    alx_str_sql_text   := TStringList.Create();
    alx_str_sql_text.Add('UPDATE  CONTAS_BANCARIAS SET CB_NOME = : , CB_CODIGO = : , CB_AB_ID = : , CB_UN_ID = 1 , CB_ATIVO = TRUE ');
    alx_str_sql_text.Add('WHERE   CB_ID = : ; ') ;
    self.ADOQuery_Apoio.Active:= False;
    self.ADOQuery_Apoio.SQL.Clear();
    self.ADOQuery_Apoio.SQL:= alx_str_sql_text;
    self.ADOQuery_Apoio.Parameters[0].Value:= alx_str_cb_nome;
    self.ADOQuery_Apoio.Parameters[1].Value:= alx_str_cb_codigo;
    self.ADOQuery_Apoio.Parameters[2].Value:= alx_int_cb_ab_id;
    self.ADOQuery_Apoio.Parameters[3].Value:= alx_int_cb_id;
    //--------------
    self.ADOQuery_Apoio.ExecSQL();
    alx_boo_apoio:= True;
    self.alx_int_conta_bancaria_id_em_foco:= alx_int_cb_id;                     //... informa o id do usuário que foi inserido ...
    //--------------
  except
    on E: Exception do begin
      alx_boo_apoio                  := False;
      self.alx_int_conta_bancaria_id_em_foco:= 0;
      ShowMessage('Ôpa, ocorreu um erro ao realizar a transação que altera uma conta bancária escolhida no sistema! Veja: ' + E.Message);
    end;
  end;
  result:= alx_boo_apoio;
end;
//==============================================================================
procedure Talx_frm_sgc_contas_bancarias.alx_voi_Colocar_Cursor_Sobre_Conta_Bancaria_Inserida_Ou_Alterada();
var
  alx_int_apoio: Integer;
begin
  try
    self.ADOQuery_Contas_Bancarias.First();
    while not(self.ADOQuery_Contas_Bancarias.Eof) do begin
      alx_int_apoio:= self.ADOQuery_Contas_Bancarias.Fields[0].AsInteger;
      if (alx_int_apoio = self.alx_int_conta_bancaria_id_em_foco) then begin
        break;
      end
      else begin
        self.ADOQuery_Contas_Bancarias.Next();
      end;
    end;
  except
    on e:Exception do begin
      showmessage('Ôpa, ocorreu um erro ao colocar o cursor sobre a Conta Bancária recém inserida ou alterada! Veja: ' + E.Message);
    end;
  end;
end;
//==============================================================================
function  Talx_frm_sgc_contas_bancarias.alx_int_Pegar_Agencia_Bancaria_Id(alx_str_agencia_bancaria_nome: String): Integer;
var
  alx_str_sql_text: TStrings;
  alx_int_apoio   : Integer;
begin
  try
    alx_str_sql_text:= TStringList.Create();
    alx_str_sql_text.Add('SELECT  AB_ID  FROM  AGENCIAS_BANCARIAS  WHERE  AB_NOME = : ');
    self.ADOQuery_Apoio.Active:= False;
    self.ADOQuery_Apoio.SQL.Clear();
    self.ADOQuery_Apoio.SQL:= alx_str_sql_text;
    self.ADOQuery_Apoio.Parameters[0].Value:= alx_str_agencia_bancaria_nome;
    self.ADOQuery_Apoio.Active:= True;
    //-----------------
    alx_int_apoio:= self.ADOQuery_Apoio.Fields[0].AsInteger;
    self.ADOQuery_Apoio.Active:= False;
    //-----------------
  except
    on E: Exception do begin
      alx_int_apoio:= 0;
      showmessage('Ôpa, ocorreu um erro ao buscar o código de identificação de uma Agência Bancária escolhida! Veja: ' + E.Message);
    end;
  end;
  result:= alx_int_apoio;
end;
//==============================================================================
function  Talx_frm_sgc_contas_bancarias.alx_int_Pegar_Banco_Sigla(alx_str_agencia_bancaria_nome: String): String;
var
  alx_str_sql_text: TStrings;
  alx_str_apoio   : String;
begin
  try
    alx_str_sql_text:= TStringList.Create();
    alx_str_sql_text.Add('SELECT  BAN_SIGLA  FROM  AGENCIAS_BANCARIAS, BANCOS  WHERE  AB_BAN_ID = BAN_ID  AND AB_NOME = : ');
    self.ADOQuery_Apoio.Active:= False;
    self.ADOQuery_Apoio.SQL.Clear();
    self.ADOQuery_Apoio.SQL:= alx_str_sql_text;
    self.ADOQuery_Apoio.Parameters[0].Value:= alx_str_agencia_bancaria_nome;
    self.ADOQuery_Apoio.Active:= True;
    //-----------------
    alx_str_apoio:= self.ADOQuery_Apoio.Fields[0].AsString;
    self.ADOQuery_Apoio.Active:= False;
    //-----------------
  except
    on E: Exception do begin
      alx_str_apoio:= 'erro!';
      showmessage('Ôpa, ocorreu um erro ao buscar a sigla do Banco associado à Agência Bancária escolhida! Veja: ' + E.Message);
    end;
  end;
  result:= alx_str_apoio;
end;
//==============================================================================
procedure  Talx_frm_sgc_contas_bancarias.alx_voi_Especificar_Cidade_E_UF_Da_Conta_Escolhida(alx_str_agencia_bancaria_nome: String);
var
  alx_str_sql_text: TStrings;
begin
  try
    alx_str_sql_text:= TStringList.Create();
    alx_str_sql_text.Add('SELECT  CITY_NOME , UF_SIGLA FROM  AGENCIAS_BANCARIAS, CIDADES, UNIDADES_FEDERACAO ');
    alx_str_sql_text.Add('WHERE   AB_CITY_ID = CITY_ID  AND CITY_UF_ID = UF_ID AND AB_NOME = : ');
    self.ADOQuery_Apoio.Active:= False;
    self.ADOQuery_Apoio.SQL.Clear();
    self.ADOQuery_Apoio.SQL:= alx_str_sql_text;
    self.ADOQuery_Apoio.Parameters[0].Value:= alx_str_agencia_bancaria_nome;
    self.ADOQuery_Apoio.Active:= True;
    //-----------------
    self.Edit6.Text:= self.ADOQuery_Apoio.Fields[0].AsString;
    self.Edit7.Text:= self.ADOQuery_Apoio.Fields[1].AsString;
    self.ADOQuery_Apoio.Active:= False;
    //-----------------
  except
    on E: Exception do begin
      self.Edit6.Text:= 'erro!';
      self.Edit7.Text:= 'erro!';
      showmessage('Ôpa, ocorreu um erro ao especificar o nome da cidade e a sigla do estado associados à Agência Bancária escolhida! Veja: ' + E.Message);
    end;
  end;
end;
//==============================================================================
procedure Talx_frm_sgc_contas_bancarias.alx_voi_Atualizar_Campos_Ao_Selecionar_Conta_Bancaria();
var
  alx_str_apoio: String;
begin
  alx_str_apoio:= self.ComboBox1.Text;
  self.Edit5.Text:= inttostr(self.alx_int_Pegar_Agencia_Bancaria_Id(alx_str_apoio));
  self.Edit8.Text:= self.alx_int_Pegar_Banco_Sigla(alx_str_apoio);
  self.alx_voi_Especificar_Cidade_E_UF_Da_Conta_Escolhida(alx_str_apoio);
end;
//==============================================================================
procedure Talx_frm_sgc_contas_bancarias.alx_voi_Controlar_Processo_Alteracao_Conta_Bancaria();
begin
  if((self.BitBtn12.Caption = 'alterar') and (self.alx_boo_Usuario_Tem_Permissao_Para_Alteracao_Conta_Bancaria()) ) then begin
    self.alx_voi_Liberar_Edits_Para_Edicao(True);                               //... liberar edits para edição ...
    self.alx_voi_Bloquear_Botoes_Para_Alteracao(True);                          //... bloquear botões ...
    self.alx_voi_Popular_Combobox_Agencias_Bancarias();                         //... popular o combobox de agências bancárias ...
    self.BitBtn12.Caption:= 'salvar';
  end
  else if ((self.BitBtn12.Caption = 'salvar') and (self.alx_boo_Verificacao_Ok()) ) then begin   //...testar antes os campos...
    if(self.alx_boo_Conta_Bancaria_Alterada_Com_Sucesso()) then begin           //... somente se a inserção foi um sucesso ...
      self.alx_voi_Popular_Grid_Contas_Bancarias();                             //... realiza um refresh no grid ...
      self.alx_voi_Colocar_Cursor_Sobre_Conta_Bancaria_Inserida_Ou_Alterada();  //... coloca o cursor sobre o usuário recém inserido
      self.alx_voi_Atualizar_Cartao_Conta_Bancaria();                           //... atualiza o cartão d usuário ...
      self.alx_voi_Liberar_Edits_Para_Edicao(False);                            //... não-liberar edits para edição ...
      self.alx_voi_Bloquear_Botoes_Para_Alteracao(False);                       //... desbloquear botões ...
      self.BitBtn12.Caption:= 'alterar';
    end;
  end;
end;
//==============================================================================
Assinar:
Postar comentários (Atom)

2 comentários:
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Celular, I hope you enjoy. The address is http://telefone-celular-brasil.blogspot.com. A hug.
Olá Alexandre!
Volte a postar no blog =(
Postar um comentário