0

当我尝试编写我的 vb6 登录方法时,我收到了这个错误。我检查了所有记录集,但看不到任何打开的记录集。为什么我会收到此错误?此登录程序的逻辑是,当用户在文本字段中输入用户名和密码时,我根据数据库(ms 访问)值检查是否正确,如果输入无效的用户名和密码 3x,则终止程序。当我尝试我的程序时,它正在使用正确的用户名和密码,但如果我尝试输入 3 次错误输入,它只会在第二次尝试无效输入时给我该错误。

有人能帮我吗?

这是我的代码:

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
4

1 回答 1

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
于 2013-03-06T10:59:23.520 回答