当我只调用下面的代码一次的时候,没有问题,
两次以上就出现问题了,
实时错误 '3705'
对象打开时,不允许操作
我就不知道,如何显示数据后关闭数据源,头疼啊
请大侠指点,谢谢先!代码如下:
    Public g_connFast As New ADODB.Connection
    Public g_rsFast As New ADODB.Recordset    Public Function Getdata(sqlcode As String)
    dim sqlfromdata as string
    sqlfromdata = "select * from DATA where pro_ID like '%" & sqlcode & "%'"
    g_connFast.CursorLocation = adUseClient
    g_connFast.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& dbaddress & ";Persist Security Info=False;"
    g_connFast.Open
    g_rsFast.Open sqlfromdata, g_connFast, adOpenStatic, adLockReadOnly    
    
    Set frmFirst.DataGrid1.DataSource = g_rsFast    End Function

解决方案 »

  1.   

    datagrid显示数据你试试吧,好用的
    记得要引用adoDim WithEvents adoPrimaryRS As Recordset '数据库连接对象
    Private Sub Command1_Click()
       'strsql 是你的查询语句 你可以order by 排序的字段
       'strsql="select xh as 学号 from 表" 这里的学号就是你datagrid中列的标题   strsql="select 字段 from 表"
       Set Db = New Connection
       Db.CursorLocation = adUseClient
       '下面的连接数据字符串你要修改一下
       Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\计划管理系统.mdb;Persist Security Info=False"
       Set adoPrimaryRS = New Recordset
       adoPrimaryRS.Open strsql, Db, adOpenStatic, adLockOptimistic
       Set DataGrid1.DataSource = adoPrimaryRS
    end sub
      

  2.   

    打开连接前先判断一下它的状态,如果已经打开,则将它关闭:Public Function Getdata(sqlcode As String)
        Dim sqlfromdata As String
        sqlfromdata = "select * from DATA where pro_ID like '%" & sqlcode & "%'"
        g_connFast.CursorLocation = adUseClient
        g_connFast.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbaddress & ";Persist Security Info=False;"
        
        If g_connFast.state = adstateopen Then
            g_connFast.Close
            g_connFast.Open
        End If
        If g_rsFast.state = adstateopen Then g_rsFast.Close
        
        g_rsFast.Open sqlfromdata, g_connFast, adOpenStatic, adLockReadOnly
        
        Set frmFirst.DataGrid1.DataSource = g_rsFastEnd Function
      

  3.   

    faysky2()  
     你的方法调试没有通过,
    不过修改了一下就可以了,谢谢~~~~顺便再问个问题哦,呵呵1.什么查询语句可以直接查出 列的名称 啊,就是 列的标题,
    2.从主界面(使用了datagrid控件,而且显示了数据)中调出 子界面(使用了me.hide    frmInfoAdd.show 语句),在这个子界面中填写信息,然后我点击“添加信息”按钮,确认已经把数据加入了数据库;接着,点击“返回”按钮(unload me      frmMain.show );可是datagrid控件不能显示我刚刚添加的信息。不知道如何解决。请教高手~~~~
    谢谢~~~
        Public Function Getdata(sqlcode As String)
        Dim sqlfromdata As String    If g_connFast.State = adStateOpen Then g_connFast.Close
        If g_rsFast.State = adStateOpen Then g_rsFast.Close    sqlfromdata = "select * from DATA where pro_ID like '%" & sqlcode & "%'"
        g_connFast.CursorLocation = adUseClient
        g_connFast.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbaddress & ";Persist Security Info=False;"
        
        g_rsFast.Open sqlfromdata, g_connFast, adOpenStatic, adLockReadOnly
        
        Set frmFirst.DataGrid1.DataSource = g_rsFastEnd Function
     
      

  4.   

    还有问题哦,呵呵
    在datagrid控件中,鼠标选定了5行记录,
    可不可以将这5行记录中数据输出到EXCLE 或WORD?
    有代码可以参考么?谢谢下面代码中,好像只可以插入字符输出,怎么使用变量输出就不懂了,请高手指点。    Dim Wapp As Object  '定义变量Wapp为Word对象
        Dim sqltemp As String
        Set Wapp = CreateObject("Word.application") '设置变量Wapp为Word对象
        
        'Wapp.Visible = True
        Wapp.Documents.Add '打开一个Word空文档    
        
        Wapp.Selection.InsertAfter ("fdfds") '插字符串
        
        Wapp.ActiveDocument.SaveAs ("e:\hello2") '存盘
        Wapp.ActiveDocument.Close '关闭Word文件
        Wapp.Quit '退出Word
        
        Set Wapp = Nothing '释放内存
      

  5.   

    1.什么查询语句可以直接查出 列的名称 啊,就是 列的标题,
    ====================================================
    g_connFast(0).Name '得到第1列标题
    g_connFast(1).Name '得到第2列标题
      

  6.   

    2.从主界面(使用了datagrid控件,而且显示了数据......
    -------------------------------------------------
    代码中缺少打开数据库连接语句:
    ......
    g_connFast.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbaddress & ";Persist Security Info=False;"
    g_connFast.Open  '<----------------------缺少这句
    g_rsFast.Open sqlfromdata, g_connFast, adOpenStatic, adLockReadOnly
    ......
      

  7.   

    在点击“返回”按钮(unload me      frmMain.show );的时候,在Form_Unload的调用 Getdata函数,刷新DataGrid
      

  8.   

    下面代码中,好像只可以插入字符输出,怎么使用变量输出就不懂了
    --------------------------------------------
    ' Wapp.Selection.InsertAfter ("fdfds") '直接插字符串dim str as string
    str="fdfds"
    Wapp.Selection.InsertAfter str '使用变量来完成插字符串
      

  9.   

    2.从主界面(使用了datagrid控件,而且显示了数据......
    -------------------------------------------数据库的连接我用代码在主界面中Form_Load()调用的,
    不知道怎么刷新datagrid控件中的数据哦,在子界面中可以用Form_Unload来完成么?
      

  10.   

    不知道怎么刷新datagrid控件中的数据哦,在子界面中可以用Form_Unload来完成么?
    ---------------------------------------------------------------------
    你写的 Getdata()过程就是刷新DataGrid了,你在子界面添加了数据(数据库已经更新),需要重新查询数据,以便更新DataGrid的显示,所以,你在子界面的窗体Unload事件里调用主界面的Getdata()过程就行了Private Sub Form_Unload()
       主界面.Getdata (参数)
    End Sub