简体   繁体   中英

how to create user login form using vba

Hello everyone i am new to VBA, i tried creating a login page that enables user login to my excel worksheet with help from some online code. i have some set of username in the worksheet containing login name, password, login attempt and status of login either failed or successful. The program checks this worksheet and grant access if the username and password matches one entered by user, however when i run the code it flags various errors. i would like the community to help.

    Public Username As String
Public Password As String
Public i As Integer
Public j As Integer
Public u As String
Public p As String

Private Sub CommandButton1_Click()

    Application.ScreenUpdating = False
    If Trim(TextBox1.Text) = "" And Trim(TextBox2.Text) = "" Then
        MsgBox "Enter username and password.", vbOKOnly
        ElseIf Trim(TextBox1.Text) = "" Then
        MsgBox "Enter the username ", vbOKOnly
        ElseIf Trim(TextBox2.Text) = "" Then
        MsgBox "Enter the Password ", vbOKOnly
    Else
        Username = Trim(TextBox1.Text)
        Password = Trim(TextBox2.Text)
        i = 1
        Do While Cells(1, 1).Value <> ""
            j = 1
            u = Cells(i, j).Value
            j = j + 1
            p = Cells(i, j).Value
            If Username = u And Password = p And Cells(i, 3).Value = "fail" Then
                MsgBox "Your Account temporarily locked", vbCritical
                Exit Do
                Else
                If Username = "u" And Password = "p" Then
                Call clear
                UserForm1.Hide
                UserForm2.Label1.Caption = u
                UserForm2.Label1.ForeColor = &H8000000D
                UserForm2.Show
                Exit Do
                Else
                If Username <> u And Password = p Then
                MsgBox "Username not matched", vbCritical + vbOKCancel
                Exit Do
                Else
                If Username = u And Password <> p Then
                If Cells(i, 3).Value = "fail" Then
                    MsgBox "Your account is blocked", vbCritical + vbOKCancel
                    Exit Do
                    Else
                    If Cells(i, 4).Value < 2 Then
                    MsgBox "Invalid password", vbCritical
                    Cells(i, 4).Value = Cells(i, 4) + 1
                    Exit Do
                Else
                    Cells(i, 4).Value = Cells(i, 4) + 1
                    Cells(i, 3).Value = "fail"
                    Cells(i, 2).Interior.ColorIndex = 3
                    Exit Do
                End If
            Else
                i = i + 1
            End If
        Loop
    End If
    Application.ScreenUpdating = True
End Sub
Sub clear()
    TextBox1.Value = ""
    TextBox2.Value = ""
End Sub
Private Sub TextBox1_Enter()
    With TextBox1
        .Back Color = &H8000000E
        .Fore Color = &H80000001
        .Border Color = &H8000000D
        TextBox1.Text = ""
    End With

End Sub
Private Sub TextBox1_AfterUpdate()
    If TextBox1.Value = "" Then
        TextBox1.BorderColor = RGB(255, 102, 0)
    End If
    i = 1
    Do Until IsEmpty(Cells(i, 1).Value)
        If TextBox1.Value = Cells(i, 1).Value Then
            With TextBox1
                .Border Color = RGB(186, 214, 150)
                .Back Color = RGB(216, 241, 211)
                .Fore Color = RGB(81, 99, 51)
            End With
        End If
        i = i + 1
    Loop
End Sub
Private Sub TextBox2_Enter()
    With TextBox2
        .Back Color = &H8000000E
        .Fore Color = &H80000001
        .Border Color = &H8000000D
    End With
    TextBox2.Text = ""
End Sub
Private Sub TextBox2_AfterUpdate()
    i = 1
    Username = TextBox1.Value
    Password = TextBox2.Value
    If TextBox2.Text = "" Then
        TextBox2.BorderColor = RGB(255, 102, 0)
    End If
    Do Until IsEmpty(Cells(i, 1).Value)
        j = 1
        u = Cells(i, j).Value
        j = j + 1
        p = Cells(i, j).Value
        If Username = u And Password = p Then
            With TextBox2
                .Border Color = RGB(186, 214, 150)
                .Back Color = RGB(216, 241, 211)
                .Fore Color = RGB(81, 99, 51)
            End With
            Exit Do
            Else
            If Username = u And Password <> p Then
            TextBox2.BorderColor = RGB(255, 102, 0)
            Exit Do
        Else
            i = i + 1
        End If
    Loop
End Sub
Sub settings()
    With UserForm1
        TextBox1.ForeColor = &H8000000C
        TextBox2.ForeColor = &H8000000C
        TextBox1.BackColor = &H80000004
        TextBox2.BackColor = &H80000004
        TextBox1.Text = "Username"
        TextBox2.Text = "Password"
        TextBox1.BorderColor = RGB(0, 191, 255)
        TextBox2.BorderColor = RGB(0, 191, 255)
        CommandButton1.SetFocus
    End With
End Sub
Private Sub UserForm_Initialize()
    Call settings
End Sub

Here's a quick rewrite that is absolutely not guaranteed to work (there could be other issues in here that aren't just syntax), but corrects the syntax issues that are making it not compile and hard to debug:

1) If blocks must contain an End If. In your code have switched everything that looks like:

Else
    If <some condition> Then

to:

ElseIf <some Condition> Then

2) You have some of your property names containinng spaces like .Back Color and the like. Those have been corrected:

Public Username As String
Public Password As String
Public i As Integer
Public j As Integer
Public u As String
Public p As String

Private Sub CommandButton1_Click()

    Application.ScreenUpdating = False
    If Trim(TextBox1.Text) = "" And Trim(TextBox2.Text) = "" Then
        MsgBox "Enter username and password.", vbOKOnly     
    ElseIf Trim(TextBox1.Text) = "" Then
        MsgBox "Enter the username ", vbOKOnly
    ElseIf Trim(TextBox2.Text) = "" Then
        MsgBox "Enter the Password ", vbOKOnly
    Else
        Username = Trim(TextBox1.Text)
        Password = Trim(TextBox2.Text)
        i = 1
        Do While Cells(1, 1).Value <> ""
            j = 1
            u = Cells(i, j).Value
            j = j + 1
            p = Cells(i, j).Value
            If Username = u And Password = p And Cells(i, 3).Value = "fail" Then
                MsgBox "Your Account temporarily locked", vbCritical
                Exit Do
            'Changed to ElseIf  
            'I don't think you want the double quotes here. 
             'I'm betting you want the variable `u` and variable `p` 
             'but with the double quotes you are literally checking if `Username` is equal to the letter "u"
            ElseIf Username = "u" And Password = "p" Then
                    Call clear
                    UserForm1.Hide
                    UserForm2.Label1.Caption = u
                    UserForm2.Label1.ForeColor = &H8000000D
                    UserForm2.Show
                    Exit Do
            'Changed to ElseIf
            ElseIf Username <> u And Password = p Then
                    MsgBox "Username not matched", vbCritical + vbOKCancel
                    Exit Do
            'Changed to ElseIf
            ElseIf Username = u And Password <> p Then
                    If Cells(i, 3).Value = "fail" Then
                        MsgBox "Your account is blocked", vbCritical + vbOKCancel
                        Exit Do
                    'Changed to ElseIf
                    ElseIf Cells(i, 4).Value < 2 Then
                        MsgBox "Invalid password", vbCritical
                        Cells(i, 4).Value = Cells(i, 4) + 1
                        Exit Do
                    Else
                        Cells(i, 4).Value = Cells(i, 4) + 1
                        Cells(i, 3).Value = "fail"
                        Cells(i, 2).Interior.ColorIndex = 3
                        Exit Do
                    End If
            Else
                i = i + 1
            End If
        Loop
    End If
    Application.ScreenUpdating = True
End Sub


Sub clear()
    TextBox1.Value = ""
    TextBox2.Value = ""
End Sub

Private Sub TextBox1_Enter()
    With TextBox1
        .Back Color = &H8000000E
        .Fore Color = &H80000001
        .Border Color = &H8000000D
        TextBox1.Text = ""
    End With

End Sub

Private Sub TextBox1_AfterUpdate()
    If TextBox1.Value = "" Then
        TextBox1.BorderColor = RGB(255, 102, 0)
    End If
    i = 1
    Do Until IsEmpty(Cells(i, 1).Value)
        If TextBox1.Value = Cells(i, 1).Value Then
            With TextBox1
                'Corrected strange space between Back and Color and so on
                .BorderColor = RGB(186, 214, 150)
                .BackColor = RGB(216, 241, 211)
                .ForeColor = RGB(81, 99, 51)
            End With
        End If
        i = i + 1
    Loop
End Sub

Private Sub TextBox2_Enter()
    With TextBox2
        'Corrected strange space between Back and Color and so on
        .BackColor = &H8000000E
        .ForeColor = &H80000001
        .BorderColor = &H8000000D
    End With
    TextBox2.Text = ""
End Sub

Private Sub TextBox2_AfterUpdate()
    i = 1
    Username = TextBox1.Value
    Password = TextBox2.Value
    If TextBox2.Text = "" Then
        TextBox2.BorderColor = RGB(255, 102, 0)
    End If
    Do Until IsEmpty(Cells(i, 1).Value)
        j = 1
        u = Cells(i, j).Value
        j = j + 1
        p = Cells(i, j).Value
        If Username = u And Password = p Then
            With TextBox2
                'Corrected strange space between Back and Color and so on
                .BorderColor = RGB(186, 214, 150)
                .BackColor = RGB(216, 241, 211)
                .ForeColor = RGB(81, 99, 51)
            End With
            Exit Do
        'Changed to ElseIf
        ElseIf Username = u And Password <> p Then
            TextBox2.BorderColor = RGB(255, 102, 0)
            Exit Do
        Else
            i = i + 1
        End If
    Loop
End Sub

Sub settings()
    With UserForm1
        TextBox1.ForeColor = &H8000000C
        TextBox2.ForeColor = &H8000000C
        TextBox1.BackColor = &H80000004
        TextBox2.BackColor = &H80000004
        TextBox1.Text = "Username"
        TextBox2.Text = "Password"
        TextBox1.BorderColor = RGB(0, 191, 255)
        TextBox2.BorderColor = RGB(0, 191, 255)
        CommandButton1.SetFocus
    End With
End Sub

Private Sub UserForm_Initialize()
    Call settings
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM