我做了用户登录界面,但运行总是提示对象变量或with变量未设置,帮我看一下,谢谢
通用声明Public OK As Boolean
Private Sub cmdOK_Click()
    Dim txtSQL As String
    Dim mrc As ADODB.Recordset
    Dim MsgText As String
    If Trim(txtUserName.Text = "") Then
        MsgBox "用户名不能为空!", vbOKOnly + vbExclamation, "警告"
        txtUserName.SetFocus
    Else
        txtSQL = "select * from 用户登录2 where name = '" & txtUserName.Text & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        If mrc.EOF = True Then提示错误
            MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
            txtUserName.SetFocus
        Else
            If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
                 OK = True
                If Trim(mrc.Fields(2)) = "管理员" Then
                  qx = "1"
                End If
                
                mrc.Close
                Me.Hide
                username = Trim(txtUserName.Text)
                
            Else
                MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
                txtPassword.SetFocus
                txtPassword.Text = ""
            End If
        End If
    End If
    
    miCount = miCount + 1
    If miCount = 3 Then
        Me.Hide
    End If
    Exit Sub
End Sub

解决方案 »

  1.   

    没看出啥原因啊,引用ADO库了吗
      

  2.   

    当EOF和BOF的值相等时 说明取出的数据为空 否则就是存在数据 
      

  3.   

    估计对象变量mrc = ExecuteSQL(txtSQL, MsgText)没有赋到值,仍为Nothing
      

  4.   

    把这个 "   Set mrc = ExecuteSQL(txtSQL, MsgText) " 中的 ExecuteSQL 方法贴出来看看
    应该是这里方法里面出问题了.
      

  5.   

    ExecuteSQL
    Public Function ExecuteSQL(ByVal SQL _
       As String, MsgString As String) _
       As ADODB.Recordset
       Dim cnn As ADODB.Connection
       Dim rst As ADODB.Recordset
       Dim sTokens() As String
       
       On Error GoTo ExecuteSQL_Error
       
       sTokens = Split(SQL)
       Set cnn = New ADODB.Connection
       cnn.Open ConnectString
       If InStr("INSERT,DELETE,UPDATE", _
          UCase$(sTokens(0))) Then
          cnn.Execute SQL
          MsgString = sTokens(0) & _
             " query successful"
       Else
          Set rst = New ADODB.Recordset
          rst.Open Trim$(SQL), cnn, _
             adOpenKeyset, _
             adLockOptimistic
          Set ExecuteSQL = rst
          MsgString = "查询到" & rst.RecordCount & _
             " 条记录 "
       End If
    ExecuteSQL_Exit:
       Set rst = Nothing
       Set cnn = Nothing
       Exit Function
       
    ExecuteSQL_Error:
       MsgString = "查询错误: " & _
          Err.Description
       Resume ExecuteSQL_Exit
    End Function