可能是我的问题太长了,实际上是这样的
我用记录集取数据的时候,把SQL语句当做字符发送到记录集的source对象执行
在测试的时候,数据库连接成功,但是执行到这条语句的时候
Set userRst = GetSql(strQuery)   
    '向getsql函数发送一条sql语句,strquery为SQL语句,然后赋给记录集useRst
If userRst.RecordCount = 1 Then
   If txtPassWord.Text = userRst.Fields("userpassword") Then
   Load MDIfrmMain
   MDIfrmMain.Show
   Unload Me
Else
   MsgBox "密码错误"
   Exit Sub
End If
就是if语句的时候,提示"实时错误‘3704’,对象关闭时,不允许操作"
很郁闷,数据库连接是成功的,请问这是什么原因。万分感谢!!!!!

解决方案 »

  1.   

    你的GetSql函数里已经把记录集关闭了吧?你把rs.Close那句注释掉,再试试
      

  2.   

    没有关闭呀,下面是GetSql函数的代码
    Public Function GetSql(ByVal strSql As String) As ADODB.Recordset
    On Error GoTo linkerr
        Dim Rst As New ADODB.Recordset
        Rst.Source = strSql
        Rst.ActiveConnection = Con
        Rst.CursorType = adOpenKeyset
        Rst.LockType = adLockOptimistic
        Rst.Open
        Set Rst = GetSql
        Exit Function
    linkerr:
        MsgBox "错误代码:" & Err.Number & "错误描述:" & Err.Description, vbCritical + vbOKOnly, "错误提示"
    End Function
      

  3.   

    Rst.ActiveConnection = Con,其中的Con打开了没有?有没有在其他地方关闭了?
      

  4.   

    也没有关闭,下面是连接函数Public Function Link() As Boolean
    On Error GoTo linkerr
        Set Con = New ADODB.Connection
        Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\data\milk.mdb;Persist Security Info=False"
        Con.CursorLocation = adUseClient
        Con.CommandTimeout = 15
        Con.Open
        Link = True
        Exit Function
    linkerr:
        Link = False
        MsgBox "数据连接错误"
    End Function
      

  5.   

    另外,我是在sub main()函数中进行连接的
    Public Sub main()
        If Link = True Then
            MsgBox "数据库连接成功"
            Load frmLogin
            frmLogin.Show
        Else
            MsgBox "数据库连接失败"
        End If
    End Sub
    不知这样可否
      

  6.   

    你在If userRst.RecordCount = 1 Then这句前加上MsgBxo userRst.State 测试记录集是否打开
    MsgBxo userRst.State 
    If userRst.RecordCount = 1 Then
    ....
      

  7.   

    'GetSql函数Set Rst=Getsql这句有问题,赋值方向搞反了,应该是: 
    Set Getsql=Rst
      

  8.   

    winehero(编程人生) 说得对,问题就出在那了,我到是没注意到