我现在编译运行程序总是出现 If mrc.EOF = True Then 提示对象变量或WITH快变量未定义,或者提示 Do While Not mrc.EOF 有错。这是为什么呀?
为了让各位大人看得更清楚的,这个程序的源码如下: http://www.sxpta.com/gyjy/down/code_vb.rar
我再把部分代码贴出来:Option Explicit
Dim mrc As ADODB.Recordset
Dim MsgText As String
Public txtSQL As String
Private Sub Form_Load()
   
    ShowTitle
    ShowData
    flagMedit = True
End SubPrivate Sub Form_Resize()
    If Me.WindowState <> vbMinimized And fMainForm.WindowState <> vbMinimized Then
        '边界处理
        If Me.ScaleHeight < 10 * lblTitle.Height Then
            
            Exit Sub
        End If
        If Me.ScaleWidth < lblTitle.Width + lblTitle.Width / 2 Then
            
            Exit Sub
        End If
        '控制控件的位置
                
        lblTitle.Top = lblTitle.Height
        lblTitle.Left = (Me.Width - lblTitle.Width) / 2
        
        msgList.Top = lblTitle.Top + lblTitle.Height + lblTitle.Height / 2
        msgList.Width = Me.ScaleWidth - 200
        msgList.Left = Me.ScaleLeft + 100
        msgList.Height = Me.ScaleHeight - msgList.Top - 200
    End If
End SubPublic Sub FormClose()
    Unload Me
End Sub'删除记录Private Sub Form_Unload(Cancel As Integer)
    flagMedit = False
    gintMmode = 0
End Sub'显示Grid的内容Private Sub ShowData()
        Dim i As Integer
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        With msgList
        .Rows = 1
        
        Do While Not mrc.EOF
            .Rows = .Rows + 1
            For i = 1 To mrc.Fields.Count
                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
            Next i
            mrc.MoveNext
        Loop
        
        
        
    End With
    mrc.Close
End Sub
'显示Grid表头
Private Sub ShowTitle()
    Dim i As Integer
    
    With msgList
        .Cols = 6
        .TextMatrix(0, 1) = "物资编号"
        .TextMatrix(0, 2) = "物资名称"
        .TextMatrix(0, 3) = "规格型号"
        .TextMatrix(0, 4) = "类别"
        .TextMatrix(0, 5) = "计量单位"
        
        
        '固定表头
        .FixedRows = 1
                
        '设置各列的对齐方式
        For i = 0 To 5
            .ColAlignment(i) = 0
        Next i
        
        '表头项居中
        .FillStyle = flexFillRepeat
        .Col = 0
        .Row = 0
        .RowSel = 1
        .ColSel = .Cols - 1
        .CellAlignment = 4
        
        '设置单元大小
        .ColWidth(0) = 300
        .ColWidth(1) = 1000
        .ColWidth(2) = 2000
        .ColWidth(3) = 2000
        .ColWidth(4) = 1000
        .ColWidth(5) = 1000
        .Row = 1
    End With
End SubPrivate Sub msgList_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    '右键弹出
    If Button = 2 And Shift = 0 Then
        PopupMenu fMainForm.menuMaterial
    End If
    
    
End Sub谢谢各位解答,假如问题解决的话,我会另外给分的。

解决方案 »

  1.   

    这个原因一般就是txtSQL中语句不合法造成的
    比如数字类型的字段它在语句中使用了单引号前几天遇到一个问题好像和你的代码类似
    后来原因找到了,是数据库连接字符串中多了一个单引号手头没有VB,明天帮你看一下吧
      

  2.   

    ......小弟也是初学斗胆蒙一个
    是不是Dim mrc As ADODB.Recordset
    有问题?
    改成 Dim mrc As NEW ADODB.Recordset
    然后在 ShowData 中加一句
    set mrs=NEW ADODB.Recordset
    偶也是照着例子程序套地
    我看的程序 基本都是这样引用地:)
      

  3.   

    我试了,和你说的一样,你要先建一个数据库,然后再按程序中的DSN名建一个DSN才可以。
      

  4.   

    需要配置sqlserver数据库
    数据库的连接有问题
      

  5.   

    你的错误的原因是因为数据库连接初始化失败,继续运行的原因是因为
    程序中使用了错误捕捉详细解释如下返回记录集函数
    Public Function ExecuteSQL(ByVal SQL _
       As String, MsgString As String) _
       As ADODB.Recordset
    'executes SQL and returns 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
          '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          '跳转到这里后,继续执行到ExecuteSQL_Exit
    End Function在你的显示数据函数中
    Private Sub ShowData()
            Dim i As Integer
            Set mrc = ExecuteSQL(txtSQL, MsgText)     '调用模块中函数
            With msgList
            .Rows = 1
             
            Do While Not mrc.EOF             ’这里继续调用mrc.eof属性,所以出错
                .Rows = .Rows + 1
                For i = 1 To mrc.Fields.Count
                    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
                Next i
                mrc.MoveNext
            Loop
            
            
            
        End With
        mrc.Close
    End Sub出错信息如下,意思是缺少一个数据源文件,看看你的书,你是不是漏了一步
    没有建好这个数据源文件常见错误: 无效的文件 dsn 'material.dsn'
      

  6.   

    打开控制面板->管理工具->数据源->文件数据源->...