Limitar tentativas de login em um sisteminha VBA

slv_gaol

11-05-2021 19:40:29

Olá, como vão?
Sou novo na área de programação é gostaria muito de uma ajuda nesse problema:
Preciso criar um esquema para que o user possa tentar acessar a planilha 3 vezes (por exemplo) caso erre a senha e depois disso ele receberá uma mensagem para contatar o ADM.

Segue o código que eu desenvolvi, mas não está dando certo. Toda ajuda será bem vinda :)

Código: Selecionar todos

Private Sub CommandButton2_Click()
    Dim var1, var2 As Date
    var1 = Date
    var2 = Time
'cont é a variável que contará a quantidade de tentativas
Dim cont As Integer
'quant é a quantidade
Dim quant As Integer
'quantas vezes poderá tentar
quant = 3
'cont contará apartir de zero
cont = 0
'volta para tentar mais uma vez
volta:
cont = cont + 1
    If cont >= quant Then
        MsgBox "Tentativas esgotadas, contate um adm."
        TextBox1.SetFocus
        Else
            If CheckBox1 = False Then
        
                If TextBox1 = "" Then
                    MsgBox "Digite o usuário"
                    Exit Sub
                    TextBox1.SetFocus
        
                Else
                    If TextBox2 = "" Then
                        MsgBox "Digite a senha"
                        Exit Sub
                        TextBox2.SetFocus
                    End If
                End If
        
            u = 3
    
            While (Planilha4.Cells(u, 1) <> TextBox1)
                u = u + 1
                If u > 100 Then
                    MsgBox "Usuário não cadastrado"
                    Exit Sub
                End If
    
            Wend
    
            Dim G As String
    
            G = Planilha4.Cells(u, 2).Value
    
            If TextBox2 <> G Then
                MsgBox "Senha incorreta"
                
                GoTo volta
        Else


            MsgBox "Seja Bem Vindo " & TextBox1

            u = 3
            While (Planilha6.Cells(u, 1) <> "")
                u = u + 1

            Wend
            Planilha6.Cells(u, 1) = TextBox1.Value
            Planilha6.Cells(u, 2) = var1
            Planilha6.Cells(u, 3) = var2
            Planilha6.Cells(u, 4) = "Usuário"



            Planilha9.Visible = xlSheetVisible
            Sheets("MENU_SISTEMAS_USUARIOS").Activate
            ActiveWindow.DisplayWorkbookTabs = False
            Hide

        End If

    Else


        If TextBox1 = "" Then
            MsgBox "Digite o usuário"
            Exit Sub
            TextBox1.SetFocus

        Else
            If TextBox2 = "" Then
                MsgBox "Digite a senha"
                Exit Sub
                TextBox2.SetFocus
            End If
        End If

        u = 3

        While (Planilha4.Cells(u, 4) <> TextBox1)
            u = u + 1
            If u > 10 Then
                MsgBox "Admin. não cadastrado"
                Exit Sub
            End If

        Wend

        Dim Y As String

        Y = Planilha4.Cells(u, 5).Value

        If TextBox2 <> Y Then
            MsgBox "Senha incorreta"
            Exit Sub

        Else


            MsgBox "Seja Bem Vindo Admin. " & TextBox1


            u = 3
            While (Planilha6.Cells(u, 1) <> "")
                u = u + 1

            Wend
            Planilha6.Cells(u, 1) = TextBox1.Value
            Planilha6.Cells(u, 2) = var1
            Planilha6.Cells(u, 3) = var2
            Planilha6.Cells(u, 4) = "Admin."


            ActiveWindow.DisplayWorkbookTabs = True
            Planilha1.Visible = xlSheetVisible
             Planilha2.Visible = xlSheetVisible
             Planilha3.Visible = xlSheetVisible
             Planilha4.Visible = xlSheetVisible
             Planilha5.Visible = xlSheetVisible
             Planilha6.Visible = xlSheetVisible
             Planilha7.Visible = xlSheetVisible
             Planilha8.Visible = xlSheetVisible
             Planilha9.Visible = xlSheetVisible
            Sheets("MENU_SISTEMAS_ADM").Select

            Hide

            End If
        End If
    End If
End Sub

webmaster

12-05-2021 14:56:33

Colega,

Tentei entender o código, mas sem poder testá-lo fica bem chato. O que atrapalha (no meu ponto de vista) é o goto. Tente troca-lo por um While..Wend enquanto incrementa o contador. Só isso dará uma clareza melhor ao código.

De toda forma, o problema fundamental é que tudo acontece enquanto o botão é clicado. Ou seja, uma vez terminado, o ciclo reinicia, junto com o contador. Seja o que for fazer, o número de tentativas deve ser armazenado de forma global ou na planilha, algo assim:

Código: Selecionar todos

Const MaximoTentativas As Byte = 3
Private Tentativas  As Byte

Public Sub UserForm_Initialize()
    Tentativas = 1
End Sub

Public Sub Botao_Click()
    If Tentativas = MaximoTentativas Then
        MsgBox "Você excedeu o limite de tentativas. Contate o administrador"
        Exit Sub
    End If
    
    If LoginBemSucedido(usuario, senha) Then
        'tudo certo aqui
    Else
        MsgBox "Login falhou"
        Tentativas = Tentativas + 1
    End If
End Sub
O código acima não foi testado. É uma apenas uma ideia de fluxo que acredito ser válido.

slv_gaol

13-05-2021 10:20:05

Olá, tudo bem?

Segue a planilha para teste, mesmo do jeito que você me falou eu não consegui. Se puder me ajudar ficarei muito grato.

SENHA:

USER: GUILHERME
PWD: COALAS
*MARCAR CAPTION ADM*

https://drive.google.com/drive/folders/ ... sp=sharing