Tuesday 11 August 2015

Login Application in VBA Excel Macro


                                                                  
Hi am karthikeyan k, Software Developer here i post  How to create Login page in vba  with more effective way..

Design  the login page...



coding...



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  
        Else If Trim (TextBox1.Text) = "" Then  
        MsgBox "Enter the username ", vbOKOnly  
        Else If 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  
    End With   
    TextBox1.Text = ""  
End Sub  
Private Sub TextBox1_AfterUpdate ()  
    If TextBox1.Value = "" Then  
        TextBox1.BorderColor = RGB (255, 102, 0)  
    End If  
    i = 1  
    Do Until Is Empty (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 Is Empty (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
  

Excel value...

Thank You....

                                                                                                      -This article is posted by karthikeyan

No comments:

Post a Comment