这是一个登录框的部分源代码,可每次用户名输入错误后报错,问为什么。
conn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\tongji.mdb;Persist Security Info=False"
conn.Open
rs.Open "select * from xtgl where 姓名='" & Trim(Text1.Text) & "'", conn
  If rs.BOF Then
     MsgBox "没有这个姓名的账户", vbOKOnly, "信息提示"
     Text1.Text = ""
     Text1.SetFocus
  End If
  If rs("密码") <> Trim(Text2.Text) Then
      MsgBox " 请输入正确的密码", vbOKOnly, "信息提示"
      Text2.Text = ""
      Text2.SetFocus
  End If

解决方案 »

  1.   

    Private Sub cmdOk_Click()
      Dim strUserPass As String, strUsername As String
      '判断用户是否已经输入用户名和密码
      strUsername = Trim(txtname.Text)
      strUserPass = Trim(txtpass.Text)
      If strUsername = "" Then
        MsgBox "请输入用户名", vbExclamation, "登录提示"
        txtname.SetFocus
        Exit Sub
      End If
      If strUserPass = "" Then
        MsgBox "请输入密码", vbExclamation, "登录提示"
        txtpass.SetFocus
        Exit Sub
      End If
      '判断用户登录信息是否正确
      rs.Open "select * from 管理员 where 用户='" & strUsername & "'", cnn, adOpenStatic, adLockOptimistic
      If rs.EOF Then
        MsgBox "没有此用户", vbCritical, "登录错误"
        txtname.SelStart = 0
        txtname.SelLength = Len(txtname.Text)
        txtname.SetFocus
        rs.Close
        Exit Sub
      End If
      rs.Close
      rs.Open "select * from 管理员 where 用户='" & strUsername & "' and 密码='" & strUserPass & "'", cnn, adOpenStatic, adLockOptimistic
      If rs.EOF Then
        MsgBox "密码错误", vbCritical, "登录错误"
        txtpass.SelStart = 0
        txtpass.SelLength = Len(txtpass.Text)
        txtpass.SetFocus
        rs.Close
        Exit Sub
      End If
      rs.Close
      frmMain.Show
      Unload Me
    End Sub