Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
Por vilc
Posts
#69257
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.
Você não está autorizado a ver ou baixar esse anexo.
Avatar do usuário
Por Basole
Posts Avatar
#69259
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

Avatar do usuário
Por Basole
Posts Avatar
#69261
Você precisa declarar a variável a baixo FORA dos eventos, no topo no módulo
Código: Selecionar todos
Dim EditOldSelec    As String
Por vilc
Posts
#69873
Boa Noite, Como ficaria esse código de atualizar o registro, mas que ele verificasse se já existe um nome já cadastrado?
Avatar do usuário
Por Basole
Posts Avatar
#69874
Boa noite,
Desculpe não entendi, o botão atualizar só fica visivel se, ao pesquisar conter o nome na lista.
Por vilc
Posts
#69876
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.
Avatar do usuário
Por Basole
Posts Avatar
#69877
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
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord