Página 1 de 1

Botão atualizar registro

Enviado: 20 Fev 2022 às 13:53
por vilc
boa tarde, estou com dificuldade em fazer o botão atualizar registro no formulário e na tabela
quando mando atualizar ele edita sempre o mesmo registro.

Re: Botão atualizar registro

Enviado: 20 Fev 2022 às 15:31
por Basole
Siga as alterações sugeridas abaixo

....no botão Editar:
Código: Selecionar todos
Dim EditOldSelec    As String

Private Sub btn_Editar_Click()

    'edita os registros na listbox

    With Me.ListBox1
        If .ListIndex = -1 Then
            Exit Sub
        End If

        EditOldSelec = .List(.ListIndex)

        Me.TxtNome.Text = .List(.ListIndex)
        Me.TxtNome.Text = .List(.ListIndex, 0)
        Me.ComboSetor.Text = .List(.ListIndex, 1)

        BtnAtualiza.Visible = True

        btn_Editar.Enabled = False
        BtnAtualiza.Visible = True
        Btn_Salavr.Enabled = False

    End With

End Sub

....no botão Atualizar:
Código: Selecionar todos
Private Sub BtnAtualiza_Click()
    Dim id          As String
    Dim rng         As Range
    Dim linha!

    Set w = Sheets("Servidores")

    With w
        .Activate

        Set rng = .Columns(1).Find(EditOldSelec, LookIn:=xlValues, _
                                                       lookat:=xlWhole)
        If Not rng Is Nothing Then

            linha = rng.Row

            .Cells(linha, 1) = Me.TxtNome.Text
            .Cells(linha, 2) = Me.ComboSetor.Text
        End If

        MsgBox ("Registro Atualizado")

        BtnAtualiza.Visible = False
        btn_Editar.Enabled = False

        ListBox1.Clear
        Call PrencheListbox

        'Limpando os campos
        TxtNome.Value = ""
        ComboSetor.Value = ""
        TxtPesquisa.Text = ""
        TxtPesquisa.SetFocus

        Sheets("inicio").Select

    End With

End Sub


Re: Botão atualizar registro

Enviado: 20 Fev 2022 às 16:57
por vilc
Basole, fiz aqui como descrito, mas quando edita um registro ele esta salvando como se fosse um novo registro.

Re: Botão atualizar registro

Enviado: 20 Fev 2022 às 17:02
por Basole
Você precisa declarar a variável a baixo FORA dos eventos, no topo no módulo
Código: Selecionar todos
Dim EditOldSelec    As String

Re: Botão atualizar registro

Enviado: 20 Fev 2022 às 21:14
por vilc
Basole, muito obrigado. funcionou certitinho! com ajuda de todos as poucos estou fazendo esse controle.

Re: Botão atualizar registro

Enviado: 21 Mar 2022 às 21:52
por vilc
Boa Noite, Como ficaria esse código de atualizar o registro, mas que ele verificasse se já existe um nome já cadastrado?

Re: Botão atualizar registro

Enviado: 21 Mar 2022 às 22:24
por Basole
Boa noite,
Desculpe não entendi, o botão atualizar só fica visivel se, ao pesquisar conter o nome na lista.

Re: Botão atualizar registro

Enviado: 21 Mar 2022 às 22:42
por vilc
então, eu tenho os Nomes:
João
José
Antonio

se eu pegar o nome João, e editar para José, ele me avisar que já existe esse nome no banco de dados.

Re: Botão atualizar registro

Enviado: 21 Mar 2022 às 23:51
por Basole
Segue o codigo atualizado:
Código: Selecionar todos
Private Sub BtnAtualiza_Click()

    Dim id          As String
    Dim rng         As Range
    Dim linha!

    Set w = Sheets("Servidores")

    With w
        .Activate

        Set rng = .Columns(1).Find(EditOldSelec, LookIn:=xlValues, _
                                                       lookat:=xlWhole)
        If Not rng Is Nothing Then
         linha = rng.Row
            
            
        Set rng = .Columns(1).Find(Me.TxtNome.Text, LookIn:=xlValues, _
                                                       lookat:=xlWhole)
        If Not rng Is Nothing Then
             MsgBox ("Já existe Registro " & Me.TxtNome.Text & ". Tente outro nome! "): Exit Sub
        Else

            .Cells(linha, 1) = Me.TxtNome.Text
            .Cells(linha, 2) = Me.ComboSetor.Text
        End If
            
        End If

        MsgBox ("Registro Atualizado")

        BtnAtualiza.Visible = False
        btn_Editar.Enabled = False

        ListBox1.Clear
        Call PrencheListbox

        'Limpando os campos
        TxtNome.Value = ""
        ComboSetor.Value = ""
        TxtPesquisa.Text = ""
        TxtPesquisa.SetFocus

        Sheets("inicio").Select

    End With

End Sub

Re: Botão atualizar registro

Enviado: 22 Mar 2022 às 18:54
por vilc
Basole, Muito obrigado mais uma vez, funcionou muito bem....