实时错误‘91’,对象变量或with块变量未设置 
指示下面的语句出错
(Do While Not mrc.EOF)括号里的
--------------附程序------------------
Option Explicit
Public txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
Private Sub ShowTitle()
    Dim i As Integer
    
    With MSHFlexGrid1
        .Cols = 11
        .TextMatrix(0, 1) = "序号"
        .TextMatrix(0, 2) = "设备名称"
        .TextMatrix(0, 3) = "检修班组"
        .TextMatrix(0, 4) = "下票人"
        .TextMatrix(0, 5) = "下票日期"
        .TextMatrix(0, 6) = "验收人"
        .TextMatrix(0, 7) = "验收日期"
        .TextMatrix(0, 8) = "许可人"
        .TextMatrix(0, 9) = "缺陷内容"
        .TextMatrix(0, 10) = "备注"        .FixedRows = 1
                        
        .FillStyle = flexFillRepeat
        .Col = 0
        .Row = 0
        .RowSel = 1
        .ColSel = .Cols - 1
        .CellAlignment = 4
        
        .ColWidth(0) = 1000
        .ColWidth(1) = 1000
        .ColWidth(2) = 1000
        .ColWidth(3) = 1000
        .ColWidth(4) = 1000
        .ColWidth(5) = 1000
        .ColWidth(6) = 1000
        .ColWidth(7) = 1000
        .ColWidth(8) = 1000
        .ColWidth(9) = 4000
        .ColWidth(10) = 1000
        .Row = 1
        End With
End Sub
Private Sub Form_Load()
    ShowTitle
    ShowData
End Sub
Private Sub ShowData()
    Dim j As Integer
    Dim i As Integer
    Dim MsgText As String
    Dim mrc As ADODB.Recordset
    Set mrc = ExecuteSQL(txtSQL, MsgText)
        With MSHFlexGrid1
        .Rows = 1
        
-------Do While Not mrc.EOF------就是这句话!-------------
            .Rows = .Rows + 1
            For i = 1 To mrc.Fields.Count
                If Not IsNull(Trim(mrc.Fields(i - 1))) Then
                Select Case mrc.Fields(i - 1).Type
                    Case adDBDate
                        .TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
                    Case Else
                        .TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
                End Select
                End If
            Next i
            mrc.MoveNext
        Loop
       End With
    mrc.Close
   End Sub
--------------------------------------------
“模块”Public fMainForm As frmMain
Public UserName As String
Sub Main()
    Dim fLogin As New frmLogin
    fLogin.Show vbModal
    If Not fLogin.OK Then
        'Login Failed so exit app
        End
    End If
    Unload fLogin
    Set fMainForm = New frmMain
    fMainForm.Show
End Sub
Public Function ConnectString() _
   As String
'returns a DB ConnectString
   ConnectString = "Filedsn=jx.dsn;UID=sa;PWD="
End Function
Public Function ExecuteSQL(ByVal SQL _
   As String, MsgString As String) _
   As ADODB.Recordset
'executes SQL and returns Recordset
   Dim cnn As New ADODB.Connection
   Dim rst As New 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
      'rst.MoveLast     'get RecordCount
      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 FunctionPublic Function Testtxt(txt As String) As Boolean
    If Trim(txt) = "" Then
        Testtxt = False
    Else
        Testtxt = True
    End If
End Function设计一个单位的检修票管理系统,现在我能看到所有添加的结果,可是我想查询到具体哪一条内容,问题就在这,我点击查询的时候不能显示结果报错‘91’,我用单独一个窗体显示MSHFlexGrid1,同时把查询结果,也显示在内,请高手指点!!!
-------------------------------------------
查询按钮的程序,但是一点击就报错!!!
Private Sub cmdInquire_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim dd(4) As Boolean
    Dim mrc As ADODB.Recordset
    Dim sQsql As String
           
    txtSQL = "select * from glinfo where "
    If Check1(0).Value Then
        If Trim(txtsbname.Text) = "" Then
             
            txtsbname.SetFocus
            Exit Sub
        Else
        dd(0) = True
            sQsql = "sb_name = '" & Trim(txtsbname.Text) & "'"
        End If
    End If
    If Check1(1).Value Then
        If Trim(txtName.Text) = "" Then
        txtName.SetFocus
            Exit Sub
        Else
            dd(1) = True
            If dd(0) Then
                sQsql = "and xp_name = '" & txtName.Text & "'"
            Else
                sQsql = "xp_name = '" & txtName.Text & "'"
            End If
        End If
    End If
    
    If Check1(2).Value Then
        If Trim(txtClassno.Text) = "" Then
             
            txtClassno.SetFocus
            Exit Sub
        Else
            dd(2) = True
            If dd(0) Or dd(1) Then
                sQsql = "and wx_team = '" & txtClassno.Text & "'"
            Else
                sQsql = "wx_team = '" & txtClassno.Text & "'"
            End If
        End If
    End If
    
    If Not (dd(0) Or dd(1) Or dd(2) Or dd(3)) Then
        MsgBox "请设置查询方式!", vbOKOnly + vbExclamation, "警告"
        Exit Sub
    End If
            sQsql = "order by id"
                
            Set mrc = ExecuteSQL(txtSQL, MsgText)
frmglresult.txtSQL = "select * from glinfo where" & sQsql
  frmglresult.Show
 End Sub
---------------------------------------------
我的QQ  44578433   请高人指点!!!

解决方案 »

  1.   

    use master
    goif exists (select * from dbo.sysdatabases where name = 'JX') 
    drop database JX
    GOcreate database JX
    go
    use JX
    go
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[glinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[glinfo]
    GOCREATE TABLE [dbo].[user_Info] (
    [user_ID] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [user_PWD] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [user_Des] [char] (10) COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY]
    GOCREATE TABLE [dbo].[glinfo] (
            [id] [int] NOT NULL ,
    [sb_name] [char] (10) COLLATE Chinese_PRC_CI_AS  NULL ,
    [wx_team] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [xp_man] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [xp_date] [datetime] NULL ,
    [ys_man] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [ys_date] [datetime] NULL ,
    [xk_man] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [qx_content] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
    [Comment] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY]
    GO
    ------------------------------------------
    SQL语句
      

  2.   

    应该是ExecuteSQL有错误时, mrc 是nothing.
      

  3.   

    '在
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    '前加
    set mrc =new adodb.recordset
      

  4.   

    set mrc =new adodb.recordset  我加上了但是还是报同样的错
      

  5.   

    SQL语句有问题
    在Set mrc = ExecuteSQL(txtSQL, MsgText)一句处设断点,debug.print txtSQL,将输出粘到查询分析器里执行,看对不对。
      

  6.   

    呵呵!这ExecuteSQL函数编出了个超级大漏洞!!!!
    错误处理前没退出,那不出错的话,也会执行的,难道你的函数就是想要出错吗?^_^
    请在
    ExecuteSQL_Exit:
    前加上一句:Exit Function另外你这个函数,并不是总能返回recordset的,对返回值,你需检查一下,其值是否为nothing!不然还会错!
      

  7.   

    ExecuteSQL_Exit:
       Set rst = Nothing
       Set cnn = Nothing
       Exit Function
       
    ExecuteSQL_Error:
       MsgString = "查询错误: " & _
          Err.Description
       Resume ExecuteSQL_Exit
    End Function
    我是这样写的    homezj(小吉)
      

  8.   

    jtkk()  看来我是出现了 你说的问题 可是我不知道怎么改
      

  9.   

    Sorry!是我看错了!
    你这个主要的问题是ExecuteSQL函数并不总会返回recordset,在出错时

    InStr("INSERT,DELETE,UPDATE",UCase$(sTokens(0))) 满足时
    ExecuteSQL函数返回的是Nothing所以
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    mrc有可能是Nothing,就会在调用 mrc.EOF 时出现“实时错误‘91’,对象变量或with块变量未设置 ”解决办法:
    在Set mrc = ExecuteSQL(txtSQL, MsgText)之后加上一句:if mrc Is Nothing then Exit Sub
      

  10.   

    我的sql 语句 在查询分析器里执行了没有问题 就是显示数据不行了
    如果执行了  if mrc Is Nothing then Exit Sub 这样不就结束了程序?也就执行不了 下面的语句了?
      

  11.   

    在Set mrc = ExecuteSQL(txtSQL, MsgText)之后加上:if mrc Is Nothing then 
    msgbox MsgText
    Exit Sub
    end if你的SQL构造得肯定有问题,而且你的代码太乱,全局变量与局部变量、控件重名且混乱不堪,很难看出所以然,你必须单步跟踪自己分析才行。
    两个关键:
    1、txtSQL在传入ExecuteSQL前是什么值,是否是有效的SQL语句
    2、看看MsgText的错误提示是什么,有利于你的分析
      

  12.   

    我把if mrc Is Nothing then Exit Sub  改成了  If mrc Is Nothing Then    可是  在 InStr("INSERT,DELETE,UPDATE",UCase$(sTokens(0))) 上又提示了  实时错误‘9’下标越界。还 请homezj(小吉)  指教
      

  13.   

    if mrc Is Nothing then 
    msgbox MsgText
    Exit Sub
    加完了 提示end if实时错误‘9’下标越界
      

  14.   

    你Load的时候有一个ShowDataPrivate Sub Form_Load()
        ShowTitle
        ShowData
    End Sub在初次加载的时候并没有给txtSQL赋值,于是ExecuteSQL当然返回了一个错误,根本就没有任何结果。应当在Sub Main()里面或者在Load里面给txtSQL 赋一个初值,用来显示GRID,就可以了。
      

  15.   

    现在看来是数据显示有问题,不明白 为什么mrc=nothing?
      

  16.   

    我在 查询分析器中 能查到我需要的结果 可是到了程序中就是不行 为什么那个mrc=nothing?
      

  17.   

    sTokens(0)下标越界,只有一种情况:
    sTokens = Split(SQL)这句没有分割成功,即传入的SQL=""做为程序你需检查一下:为什么会给ExecuteSQL函数传入SQL查询空串?这怎么查呀?做为ExecuteSQL函数本身的完善,你应在函数最开始,即Dim sTokens() As String后,加上if len(trim(SQL))=0 then
    MsgString="查询串不能为空!"
    Exit Function
    end if
      

  18.   

    你说的是查询条件? 我设置了 在查询分析器中执行 就没问题
    你的意思是 我没设置查询条件 或者查询条件 没有设置在sql语句中?
      

  19.   

    这个代码是通过ODBC数据源连接库,所以你要确定你用了ODBC。ConnectString就不用了吧,直接写在ExecuteSQL中:Public Function ExecuteSQL(ByVal SQL _
       As String, MsgString As String) _
       As ADODB.Recordset    Dim cnn As New ADODB.Connection
        Dim rst As New ADODB.Recordset
        Dim sTokens() As String
       
        On Error GoTo ExecuteSQL_Error
       
        Set cnn = New ADODB.Connection
        cnn.Open "jx","sa",""
       
        sTokens = Split(SQL)
        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
      

  20.   

    这是我的 麻烦 看看有错吗? 
    Public Function ConnectString() _
       As String
    'returns a DB ConnectString
       ConnectString = "Filedsn=jx.dsn;UID=sa;PWD="
    End Function
    Public Function ExecuteSQL(ByVal SQL _
       As String, MsgString As String) _
       As ADODB.Recordset
    'executes SQL and returns Recordset
       Dim cnn As New ADODB.Connection
       Dim rst As New 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
          'rst.MoveLast     'get RecordCount
          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
     现在 我就是查询不到结果 查询条件我也设置了 而且 同样的查询条件 在查询分析器中执行 就没有问题 mrc=nothing  我不太明白 麻烦 高手给解释一下
      

  21.   

    在查询分析器中可正确执行只能说明你的SQL语句没有问题。同时也说明了你的代码有问题,你用我上面写的方法试试。
      

  22.   

    mrc=nothing  
    能不能 给我讲讲 为什么?
      

  23.   

    首先不再是“实时错误‘91’,对象变量或with块变量未设置”,而且可以查询到数据了,说明现在可以正确的连接数据库了。
    查询完成后,可以set mrc=nothing,用来释放内存,不过不是必须的
    数据有空行提示下标越界应该是你MSHFlexGrid显示数据的代码有问题,你先推敲一下
      

  24.   

    提示查询错误,你在ExecuteSQL中的Set rst = New ADODB.Recordset下面,试试加上:
    If rst.State = 1 Then rst.Close
    rst.CursorLocation = adUseClient
      

  25.   

    可是弹出来的错误还是‘91’ 在本地窗口里的还同时显示‘9’查询错误下标越界
    我把主要的程序贴出来 高手看看
    Option Explicit
    Public txtSQL As String
    Dim mrc As ADODB.Recordset
    Dim MsgText As StringPrivate Sub Form_Load()
        ShowTitle
        ShowData
        
    End Sub
    Private Sub ShowData()
        
        Dim j As Integer
        Dim i As Integer
        Dim MsgText As String
        Dim mrc As ADODB.Recordset
       
        Set mrc = ExecuteSQL(txtSQL, MsgText) *****我在这里设置了断点*******
    Debug.Print txtSQL  ****在本地窗口中msgtext为"",mrc为nothing****** 
       If mrc Is Nothing Then Exit Sub        With MSHFlexGrid1
            .Rows = 1
            
            Do While Not mrc.EOF
                .Rows = .Rows + 1
                For i = 1 To mrc.Fields.Count
                    If Not IsNull(Trim(mrc.Fields(i - 1))) Then
                    Select Case mrc.Fields(i - 1).Type
                        Case adDBDate
                            .TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
                        Case Else
                            .TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
                    End Select
                    End If
                Next i
                mrc.MoveNext
            Loop
         
        End With
        mrc.Close
     End Sub
    运行的结果就是在表格中显示了标题 但是没有数据,是不是还没有连接上数据库。我只能晚11点上网了,一会儿出去,不能及时回复大家。请高手指点!!!
      

  26.   

    我加了 还是什么也查询不到在     Set mrc = ExecuteSQL(txtSQL, MsgText)
    Debug.Print txtSQL  这里 还是mrc=nothing    你加的这段代码是什么作用?
      

  27.   

    If mrc Is Nothing Then Exit Sub
     我设置了这句 如果没有它  还是提示‘91’错误 同时在本地窗口中msgtext 提示“查询错误下标越界”
      

  28.   

    和小吉有同感,你的代码比较乱,看的很吃力。快上班了,也没时间,要不你从简单的开始,这样试试'模块中:
    Public Function ExecuteSQL(ByVal SQL As String) _
       As ADODB.Recordset    Dim cnn As New ADODB.Connection
        Dim rst As New ADODB.Recordset
        Dim sTokens() As String
       
    On Error GoTo ExecuteSQL_Error
        '通过ODBC数据源连接数据库
        Set cnn = New ADODB.Connection
        cnn.Open "jx","sa",""   '这里三个括号中分别是“数据源名称”,“ID”,“密码”
       
        sTokens = Split(SQL)
        If InStr("INSERT,DELETE,UPDATE", _
            UCase$(sTokens(0))) Then
            cnn.Execute SQL
        Else
           Set rst = New ADODB.Recordset
           rst.CursorLocation = adUseClient
           rst.Open Trim$(SQL), cnn, _
              adOpenKeyset, _
              adLockOptimistic
           Set ExecuteSQL = rst
        End If
    ExecuteSQL_Exit:
        Set rst = Nothing
        Set cnn = Nothing
        Exit Function
       
    ExecuteSQL_Error:
        MsgString = "查询错误: " & _
           Err.Description
        Resume ExecuteSQL_Exit
    End Function'窗体中:
    Option Explicit
        Dim txtSQL As String
        Dim mrc As ADODB.RecordsetPrivate Sub Form_Load()
        ShowTitle 
    End Sub'不用那个ShowData() 
        
    '查询按钮:
    Private Sub cmdInquire_Click()    Dim s(4) As String      
        txtSQL = "select * from glinfo where 1=1 "
        If Check1(0).Value Then
            If Trim(txtsbname.Text) = "" Then
                txtsbname.SetFocus
                s(0)=""
                Exit Sub
            Else
                s(0) = " and sb_name = '" & Trim(txtsbname.Text) & "'"
            End If
        End If
        If Check1(1).Value Then
            If Trim(txtName.Text) = "" Then
                txtName.SetFocus
                s(1)=""
                Exit Sub
            Else
                s(1) = " and xp_name = '" & txtName.Text & "'"
            End If
        End If
        If Check1(2).Value Then
            If Trim(txtClassno.Text) = "" Then
                txtClassno.SetFocus
                s(2)=""
                Exit Sub
            Else
                s(2) = "and wx_team = '" & txtClassno.Text & "'"
            End If
        End If
        s(3) = " order by id "  
        txtSQL = txtSQL & s(0) & s(1) & s(2) & s(3) 
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        set MSHFlexGrid1.DataSource =mrc   
        mrc.Close
     End Sub
      

  29.   

    上面的代码倒数第四行应该是:Set mrc = ExecuteSQL(txtSQL)
      

  30.   

    Set mrc = ExecuteSQL(txtSQL)
    提示编译错误 参数不可选