Dim sql As String
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset    sql = "select * from 电力能耗表 where 日期 between '=" & BeginDate.Value & "' and '" & EndDate.Value & "'"
    
    Set rs = ConnWZ.Execute(sql)
问题是:每次执行到Set rs = ConnWZ.Execute(sql)这条语句的时候就会报错----Run-time error '91':
object variable or with block variable not set
我在module中已经定义了connection,而且已经open了,我不知道哪里出错了,也试过很多的方法不能解决,谢谢帮助.mail:[email protected].

解决方案 »

  1.   

    if rs.state=adstateopen then rs.close
    rs.open sql,connwz,adopenkeyset,adlockreadonly
    if rs.recordcount>0 then
    ...
    end if
    set rs=nothing
      

  2.   

    对,就用rs.open sql就可以,不用rs = ConnWZ.Execute(sql)
      

  3.   

    我用过rs.open,但是执行到rs.open的时候报错如下Run-time error '3709'
    operation is not allowed on an object referencing a closed or invalid connection.
      

  4.   

    这样试试:Dim sql As String
        Dim rs As ADODB.Recordset
        Set rs = New ADODB.Recordset    sql = "select * from 电力能耗表 where 日期 between '=" & BeginDate.Value & "' and '" & EndDate.Value & "'"
        
        rs.open sql,ConnWZ,OpenDynamic ,adLockOptimistic
      

  5.   

    operation is not allowed on an object referencing a closed or invalid connection.
    不能对已经关闭或者非法的连接执行这样的操作你的连接断了,需要重新连接。其实既然连接是全局变量,最好不要断开,除非程序退出了。如果要断开,那就需要重新建立连接。
      

  6.   

    我没有断链接呀,我还特地在语句的前面用state判断是否为open,它说是open的呀
    msn地址:[email protected]
      

  7.   

    Set ConnWZ = New ADODB.Connection
        Set rs = New ADODB.Recordset
        ConnWZ.Open "dsn=Facility;uid=;pwd=;"
        ConnWZ.CursorLocation = adUseClient
        rs.CursorType = adOpenStatic
        rs.LockType = adLockOptimistic
      

  8.   

    首先看你的connWZ开了没?再 set rs.Activeconnection= connWZ .然后rs.open ...
      

  9.   

    If ConnWZ.stae = adStateOpen Then
            MsgBox "ok"
        Else
            MsgBox "no"
        End If用这段代码判断,执行到第一行时就报错Run-time error '3001'
    Arguments are of the wrong type,are out of acceptable range,or are in conflict with one another
      

  10.   

    哪位vb高手能不能加我到msn中,我一个人编程,一个人学习好困难呀.
      

  11.   

    Dim sql As String
        'Dim rs As ADODB.Recordset
        'Set rs = New ADODB.Recordset'不要    sql = "select * from 电力能耗表 where 日期 between '=" & BeginDate.Value & "' and '" & EndDate.Value & "'"
        
        Set rs = ConnWZ.Execute(sql)
      

  12.   

    是facility数据库中电力能耗表
      

  13.   

    Set ConnWZ = New ADODB.Connection 

    Dim ConnWZ = New ADODB.Connection
      

  14.   

    ConnWZ.Open "dsn=Facility;uid=;pwd=;"
        ConnWZ.CursorLocation = adUseClient
    交换一下他们的位置
      

  15.   

    rs.open sql,connwz,adopenkeyset,adlockreadonly
    打开连接问题
      

  16.   

    connWZ 是我的connection对象.我用    If ConnWZ.stae = adStateOpen 来判断是否打开,报错Run-time error '3001'
    Arguments are of the wrong type,are out of acceptable range,or are in conflict with one another
    它说明何问题是没有打开还是它根本不认识这个对象?
      

  17.   

    If ConnWZ.stae = adStateOpen 
    stae改為state
      

  18.   

    dim myConn as adodb.connection
    set myConn=new adodb.connection
    myconn.open "dsn=;uid=;,pwd="
    myConn.adcursorlocation=adUserClientdim rs as adodb.recordset
    set rs=new adodb.recordset
    rs.open "select * from 电力能耗表 where 日期 between '=" & BeginDate.Value & "' and '" & EndDate.Value & "'",myConn, adOpenstatic,adlockoptimstic
    set rs=nothing
    rs.close我想这样子应该没有问题,不过我建议你表名最好用英文啦
      

  19.   

    数据库我用的是Access,连接connection时open的参数为
    "dsn=Facility;uid=;pwd=;"
    鼠标移到recordset的ActiveConnection时,自动跳出
    rs.ActiveConnection="Provider=MSDASQL.1;Extended Properties="DSN=Facility...""
    我怀疑是不是我的Open参数写错了,Provider该指定是
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\Facility\facility.mdb;
    我不太明白的是Open参数直接写"dsn=Facility;uid=;pwd=;"它默认的provider是哪个?
    万分感谢:)
      

  20.   

    如果你用oledb的话,就这样一来好了"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=& app.path& \facility.mdb;"
     
    别的参数就不需要了你们公司里面应该有前辈啦
      

  21.   

    ConnWZ.Open Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=C:\MyDocuments\Facility\facility.mdb;
      

  22.   

    ConnWZ.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=C:\MyDocuments\Facility\facility.mdb;"
      

  23.   

    使用此code:ConnWZ.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=C:\MyDocuments\Facility\facility.mdb;"
    报错:
    Run-time error '-2147467259(80004005)':
    Could not find installable ISAM
      

  24.   

    多谢各位的指点,我的问题已经解决了.是SQL语句写错了.
    错误语句:sql = "select * from 电力能耗表 where 日期 between '=" & BeginDate.Value & "' and '" & EndDate.Value & "'"
    正确语句:sql = "select * from 电力能耗表 where 日期 between # " & BeginDate.Value & " # and # " & EndDate.Value & " #"原因:数据库为ACCESS,则日期型字段要使用#,如果数据库为SQL,则日期型字段要使用'.