我尝试编写vb6登录方法时遇到错误。 我检查所有记录集,我看不到任何打开的记录集。为什么我收到此错误? 此登录程序的逻辑是,当用户在文本字段中输入用户名和密码时,我会根据数据库(ms访问)值检查它是否正确,如果他输入了无效的用户名和密码3x,则终止该程序。 当我尝试我的程序时,它正在使用正确的用户名和密码,但如果我尝试输入3x错误,那么在第二次尝试输入无效时就会给出错误。
有人可以帮助我吗?
这是我的代码:
Dim Con As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim Rs2 As New ADODB.Recordset
Dim ctr As Integer
Dim cmdCommand As New ADODB.Command
Dim have As Integer
Dim Username As String
Dim Password As String
Private Sub cmdLogin_Click()
If ctr <> 3 Then
If txtUsername.Text = Username And txtPassword.Text = Password Then
MsgBox "Login Successful!!"
MDIForm1.Show
Unload Me
ElseIf txtUsername.Text <> Username And txtPassword.Text <> Password Then
MsgBox "Invalid Log in!"
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.SetFocus
ElseIf txtUsername.Text <> Username And txtPassword.Text = Password Then
MsgBox "Invalid Log in!"
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.SetFocus
ElseIf txtUsername.Text = Username And txtPassword.Text <> Password Then
MsgBox "Invalid Log in!"
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.SetFocus
ElseIf txtUsername.Text = "" And txtPassword.Text = "" Then
MsgBox "Invalid Log in!"
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.SetFocus
Else
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.SetFocus
End If
ctr = ctr + 1
Else
MsgBox "You are not a valid user! The Program will be terminated"
End
End If
End Sub
Private Sub Form_Load()
Call OpenConnection
With Rs
.ActiveConnection = Con
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
End With
End Sub
Private Sub OpenConnection()
If Con.State = 1 Then
Con.Close
End If
Set Con = New ADODB.Connection
Con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Users\User\Documents\dbMMM.accdb;Persist Security Info=False;"
Con.Open
End Sub
Private Sub txtUsername_LostFocus()
With Rs2
.ActiveConnection = Con
.Source = "Select * From tblUser where UserName = '" & txtUsername.Text & "' " 'The highlight of my error is in this line
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open
have = 0
If Rs2.EOF = False Then
Rs2.MoveFirst
Do
Username = Rs2.Fields(0)
Password = Rs2.Fields(1)
Rs2.MoveNext
Loop Until Rs2.EOF = True
Rs2.Close
End If
End With
End Sub
答案 0 :(得分:1)
如果用户名没有匹配项,则_LostFocus
代码会保留记录集。 (现在代码已正确缩进,这个更清晰了)
我处理这个问题的更好方法是在按下登录按钮时进行一次检查,看看是否有任何记录与用户名和密码匹配,而不是找到用户,然后手动比较密码。
With Rs2
.ActiveConnection = Con
.Source = "Select * From tblUser where UserName = '" & Replace(txtUsername.Text, "'", "''") & "' AND Password = '" & Replace(txtPassword.Text, "'", "''") & "';"
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open
LoginValid = Not .EOF
.Close
End With