这是连接数据库的代码 Dim conn As ADODB.Connection 
Dim rs As ADODB.Recordset 
Dim sql As String 
sPath = App.Path & "\TestResult.mdb" 
Set conn = New ADODB.Connection 
Set rs = New ADODB.Recordset 
conn.CursorLocation = adUseClient 
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sPath & " ';Persist Security Info=False" 
conn.Open 
sql = "select * from record" 
'rs.StayInSync = True 
rs.Open sql, conn, adOpenDynamic, adLockOptimistic 
Set MSFlexGrid1.DataSource = rs 运行提示错误:object vaiable or with block variable not set 百度查了也不知道那里的错误,没法解决了,郁闷
大家帮我看看呀~```` 

解决方案 »

  1.   

    Dim cn As New ADODB.Connection, rs As New ADODB.Recordset    If cn.State = adStateOpen Then cn.Close    cn.Provider = "Microsoft.Jet.OLEDB.4.0"    cn.ConnectionString = App.path & "\TestResult.mdb"    cn.Open    rs.Open "INSERT INTO t_gp_jsy(jsymc,iszz,xtbz) VALUES('',false,'')", cn, adOpenStatic, adLockOptimistic
      

  2.   

    还是这样的问题 sigh~
      

  3.   

    如果我用data数据控件,就没有问题
    用代码写这个问题就是解决不了,已经参考了很多人的代码了 :(
      

  4.   

    在详细说一下问题背景:
    实现查询功能,根据条件查询的结果显示在msflexgrid表格中帮帮忙呀~~~~~
      

  5.   

    这是我以前写的一个简单统计的小程序代码,也许会对你有用(要在窗体上加一个Adodc1,visible=false,其它不用设置)
    Dim cap As String
    Dim ado As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim mdbpath As StringSub initcpmc()
     Set ado = New Connection
     Set rs = New Recordset
     ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
     rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
     rs.MoveFirst
      While Not rs.EOF
       cpmc.AddItem rs("产品名称")
       rs.MoveNext
      Wend
     rs.Close
     ado.Close
    End SubPrivate Sub Command1_Click()
    If Trim(cpmc.Text) <> "" And Trim(jianshu.Text) <> "" Then
    pinming.AddItem cpmc.Text
    pingshu.AddItem jianshu.Text
    Else
    MsgBox "产品名称或产品件数不能为空", vbOKOnly And vbInformation, "Error"
    End If
    heji.Text = Val(heji.Text) + Val(zongjia.Text)
    cpmc.SetFocus
    End SubPrivate Sub Command2_Click()
     If pinming.ListCount > 0 And pingshu.ListCount > 0 Then
     Dim kk As Double
     Set ado = New Connection
     Set rs = New Recordset
     ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
     rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
     rs.MoveFirst
     rs.Find "产品名称='" & Trim(pinming.Text) & "'"
     If Not rs.EOF Then
      kk = rs("零售价") * Val(pingshu.Text)
      heji.Text = Val(heji.Text) - kk
     End If
     rs.Close
     ado.Close
     pinming.RemoveItem pinming.ListIndex
     pingshu.RemoveItem pingshu.ListIndex
     End If
    End SubPrivate Sub Command3_Click()
     Unload Me
    End Sub
    Private Sub cpmc_Click()
     Set ado = New Connection
     Set rs = New Recordset
     ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
     rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
     rs.MoveFirst
     rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
     If Not rs.EOF Then
      danjia.Text = rs("零售价")
     End If
     rs.Close
     ado.Close
     zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))End SubPrivate Sub cpmc_KeyDown(KeyCode As Integer, Shift As Integer)
     Set ado = New Connection
     Set rs = New Recordset
     ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
     rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
     rs.MoveFirst
     rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
     If Not rs.EOF Then
      danjia.Text = rs("零售价")
     End If
     rs.Close
     ado.Close
     zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))End SubPrivate Sub cpmc_LostFocus()
     'Set ado = New Connection
    ' Set rs = New Recordset
    ' ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
    ' rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
    ' rs.MoveFirst
    ' rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
     'If Not rs.EOF Then
     ' danjia.Text = rs("零售价")
    ' End If
    ' rs.Close
    ' ado.Close
    ' zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
    End SubPrivate Sub Form_Load()
    mdbpath = App.Path & "\jgtable.mdb"
    cap = Me.Caption
    Me.ShowIf Dir(mdbpath) = "" Then
    MsgBox "系统初始化失败 (-1)" & vbCrLf & "原因:未找到所用数据库" & vbCrLf & "jgtable.mdb", vbOKOnly Or vbInformation, "错误"
    Unload Me
    Else
    initcpmc
    End If
    End SubPrivate Sub jianshu_GotFocus()
    SendKeys "{HOME}+{END}"
    End SubPrivate Sub jianshu_KeyPress(KeyAscii As Integer)
     Dim tmp As Boolean
     If KeyAscii = 13 Then Command1_Click
     
      tmp = (KeyAscii > 57) Or (KeyAscii < 48) And (KeyAscii <> 8)
     If tmp Then
      KeyAscii = 0
     End If
    End SubPrivate Sub jianshu_KeyUp(KeyCode As Integer, Shift As Integer)
     zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
    End SubPrivate Sub jianshu_LostFocus()
     'zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
    End SubPrivate Sub pingshu_Click()
     Dim dd As Integer
     dd = pingshu.ListIndex
     pinming.Selected(dd) = True
    End SubPrivate Sub pinming_Click()
     Dim dd As Integer
     dd = pinming.ListIndex
     pingshu.Selected(dd) = True
    End SubPrivate Sub Timer1_Timer()
     Me.Caption = cap & " (" & Now & ")"
    End Sub