简体   繁体   中英

User login form for excel vba

I am trying to create a login form where the username and password input match the data on a sheet. I tried to code for if either the username or password is wrong, it will show a msgbox and clear the input to redo. But when I tested that function, nothing happened. Picture here 在此处输入图片说明

Code here

Private Sub login_Click()

    Dim x, a As Double

    a = WorksheetFunction.CountA(Range("A:A"))

    If untb.Value = "" Then
    MsgBox ("Enter the username"), vbOKOnly

    ElseIf passtb.Value = "" Then
    MsgBox ("Enter the password"), vbOKOnly

    ElseIf untb.Value <> "" And passtb.Value <> "" Then

    For x = 1 To a

    If untb.Value = Cells(x, 2) And passtb.Value = Cells(x, 3) Then
    Unload Me
    MsgBox ("Welcome to Great Wines :)"), vbOKOnly
    order.show

    End If

    Next x

    Else
    MsgBox ("Invalid username or password!"), vbOKOnly
    untb.Value = ""
    passtb.Value = ""
    untb.SetFocus

    End If


End Sub

You may try something like this...

Private Sub login_Click()
Dim UserName As String, PW As String
Dim rngUser As Range
Dim firstUser As String
Dim UserFound As Boolean

If untb.Value = "" Then
    MsgBox ("Enter the username"), vbOKOnly
    untb.SetFocus
    Exit Sub
End If

If passtb.Value = "" Then
    MsgBox ("Enter the password"), vbOKOnly
    passtb.SetFocus
    Exit Sub
End If

UserName = untb.Value
PW = passtb.Value
With Range("B:B")
    Set rngUser = .Find(UserName, lookat:=xlWhole)
    If Not rngUser Is Nothing Then
        firstUser = rngUser.Address
        Do
            If PW = rngUser.Offset(0, 1).Value & "" Then
                UserFound = True
                Unload Me
                MsgBox ("Welcome to Great Wines :)"), vbOKOnly
                Order.Show
            Else
                Set rngUser = .FindNext(rngUser)
            End If
        Loop While Not rngUser Is Nothing And firstUser <> rngUser.Address
    Else
        MsgBox "UserName is Incorrect!", vbExclamation, "UserName Not Found!"
        untb.Value = ""
        passtb.Value = ""
        Exit Sub
    End If
End With

If Not UserFound Then
    MsgBox "Invalid password!", vbOKOnly
    untb.Value = ""
    passtb.Value = ""
    untb.SetFocus
End If
End Sub

This should handle what you're trying to do. You will need to update the With block with your sheet name otherwise you may run into further issues

Private Sub login_Click()
    Dim LoginUser As Range
    Dim FirstLoginUserAddress As String
    Dim SuccessfulLogin As Boolean

    If untb.Value <> "" And passtb.Value <> "" Then
        ' Update with the sheet reference where your login/passwords are kept. Otherwise, You will get issues
        With Sheet1.Range("B:B")
            Set LoginUser = .Find(untb.Value)
            SuccessfulLogin = False
            If Not LoginUser Is Nothing Then
                FirstLoginUserAddress = LoginUser.Address
                Do
                    If CStr(LoginUser.Offset(0, 1).Value2) = passtb.Value Then
                        SuccessfulLogin = True
                        Exit Do
                    Else
                        Set LoginUser = .FindNext(LoginUser)
                    End If
                Loop Until LoginUser Is Nothing Or LoginUser.Address = FirstLoginUserAddress
            End If
        End With

        If SuccessfulLogin = True Then
            Unload Me
            MsgBox ("Welcome to Great Wines :)"), vbOKOnly
            Order.Show
        Else
            MsgBox ("Invalid username or password!"), vbOKOnly
            untb.Value = ""
            passtb.Value = ""
            untb.SetFocus
        End If
    Else
        MsgBox "Enter the " & IIf(untb.Value = vbNullString, "username", "password"), vbOKOnly
    End If
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