简体   繁体   中英

vb6 Login form code error

I set up a database with one entry for my login form and it worked well. After populating the same database, I can't login using the data added to the database. I'm stuck with the first entry from my table.

here is the code:

Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic

recset.MoveFirst
Do While Not recset.EOF

    If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
        usname = True

        If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
            uspass = True

            If recset("Usertype").Value = "user" Then
            main.mnuAddUser.Enabled = False
            End If

            txtPassword.Text = ""
            txtUsername.Text = ""
            main.Show
            Me.Hide
        Else
            uspass = False
            MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
            txtPassword.Text = ""
            txtUsername.Text = ""
            txtUsername.SetFocus
            Exit Do
            Exit Sub
        End If
        Exit Sub
    Else
        usname = False
        MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
        txtPassword.Text = ""
        txtUsername.Text = ""
        Exit Do
        Exit Sub
    End If

    recset.MoveNext
    Loop

    recset.Close
    connect.Close

In your code, you're looking at the first record of the recordset. Keep that in mind as you go through this Method.

IF

  1. The username matches what the user typed in, we will look at the passwords.
  2. If the passwords match, we will look at the UserType .
  3. Then, we show main , whatever that is.
  4. THEN you will exit the Sub .

ELSE

If the username doesn't match anything in the first record , the fields are cleared and we are exiting the Do Loop and the Sub .

What I would expect to see : If the usernames don't match, move on to the next record . Only at recset.EOF would I expect to be kicked out of the Sub - because only then can you say you have analyzed every record.

Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic

recset.MoveFirst
Do While Not recset.EOF

    If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
        usname = True

        If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
            uspass = True

            If recset("Usertype").Value = "user" Then
            main.mnuAddUser.Enabled = False
            End If

            txtPassword.Text = ""
            txtUsername.Text = ""
            main.Show
            Me.Hide
        Else
            uspass = False
            MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
            txtPassword.Text = ""
            txtUsername.Text = ""
            txtUsername.SetFocus
            Exit Do 
            Exit Sub
        End If
        Exit Sub
    Else
        usname = False
        MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
        txtPassword.Text = ""
        txtUsername.Text = ""
        Exit Do ' These are ending your search
        Exit Sub
    End If

    recset.MoveNext ' So you're never getting here
    Loop

    recset.Close
    connect.Close

Here is my solution

    Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic

recset.MoveFirst
Do While Not recset.EOF

    If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
        usname = True

        If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
            uspass = True

            If recset("Usertype").Value = "user" Then
                main.mnuAddUser.Enabled = False
            End If

            txtPassword.Text = ""
            txtUsername.Text = ""
            main.Show
            Me.Hide
        Else
            uspass = False
            MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
            txtPassword.Text = ""
            txtUsername.Text = ""
            txtUsername.SetFocus

        End If
    End If
recset.MoveNext
Loop
'I placed this outside the loop as it will execute wether the conditions are met or not 
If usname = False Then

        MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
        txtPassword.Text = ""
        txtUsername.Text = ""

End If

recset.Close
connect.Close`

The Method goes like this

  1. If username matches with user input then, it will check the password
  2. Getting the correct password, it will move to check the usertype
  3. If password is wrong, a prompt message will come out.
  4. If username does not match recset.Movenext will check the next line if the username matches doing the loop until recset.EOF.

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