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
UserType
. main
, whatever that is. 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
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.