Public Function ExcuteSQL(sql As String) As String
   Dim mycommection As Connection
   Dim myrecordset As Recordset
 '连接数据库
   Set myconnection = New Connection
   Set myrecordset = New Recordset
   mypath = App.Path & "\mis.mdb"
   myconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & mypath
   myrecordset.Open "[communication]", myconnection, adOpenKeyset, adLockOptimistic
   Set ExcuteSQL = myrecordset
End Function
Private Sub cmdok_Click()
Dim sql As String
 spl = "select * from communication"
 Set DataGrid1.DataSource = myrecordset
       DataGrid1.Refresh
End Sub
以上代码那里有问题?
请各位高手指点。

解决方案 »

  1.   

    myconnection.CursorLocation =adUseClient 加个这个再式一试
      

  2.   

    在通用申明里写上:
    Public myCon As new ADODB.Recordset  '用于打开连接
    Public myRes As new ADODB.Connection  '用于打开记录你的  Dim mycommection As Connection
       Dim myrecordset As Recordset
     '连接数据库
       Set myconnection = New Connection
       Set myrecordset = New Recordset
    这几条语句就不要了!!!下面你还要自己改成myCon.Open等,你自己看着办!关键是cmdok_Click()的事件,你要调用你的ExcuteSQL函数。Private Sub cmdok_Click()
    Dim sql As String
     sql = "select * from communication"
     ExcuteSQL(sql)Set DataGrid1.DataSource = myrecordset
           DataGrid1.Refresh
    End Sub
      

  3.   

    Set ExcuteSQL = myrecordset     这句改成Set myrecordset = ExcuteSQL(sql)
      

  4.   

    Public Function ExcuteSQL(sql As String) As String
    你的函数声明中声明函数返回值是STRING
    可返回的却是个RECORDSET
    你说会对吗?
      

  5.   

    贴一段测试过的
    Option Explicit
    Private conn As ADODB.Connection
    Private rs As ADODB.RecordsetPrivate Sub Form_Load()
    Dim apppath As String
    Dim DbFileName As String
    Dim ConnectString As String
    Dim i As Integer
    Set conn = New ADODB.Connection
    If Right(App.Path, 1) = "\" Then
                    apppath = App.Path
            Else
                    apppath = App.Path & "\"
    End If
    DbFileName = apppath & "article.mdb"
    ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                    DbFileName & ";Persist Security Info=False;"
                    
                    On Error Resume Next
                    With conn
                            .CursorLocation = adUseClient
                            
                            .Open ConnectString
                    End With
                    Set rs = New ADODB.Recordset
    rs.Open "select id,mc from mz", conn, 1, 3
    If rs.EOF Then
    Exit Sub
    End If
    Set DataGrid1.DataSource = rsEnd Sub
      

  6.   

    Public Function ExcuteSQL(sql As String) As String
    你的函数声明中声明函数返回值是STRING
    可返回的却是个RECORDSET改正后的代码
    Option Explicit
    Private conn As adodb.Connection
    Private rs As adodb.RecordsetPrivate Function ExcuteSQL(sql As String) As adodb.Recordset
       Dim apppath As String
    Dim DbFileName As String
    Dim ConnectString As String
    Dim i As Integer
    Set conn = New adodb.Connection
    If Right(App.Path, 1) = "\" Then
                    apppath = App.Path
            Else
                    apppath = App.Path & "\"
    End If
    DbFileName = apppath & "article.mdb"
    ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                    DbFileName & ";Persist Security Info=False;"
                    
                    On Error Resume Next
                    With conn
                            .CursorLocation = adUseClient
                            
                            .Open ConnectString
                    End With
       
       Set rs = New adodb.Recordset
       rs.Open sql, conn, 1, 3
        If rs.EOF Then
            Exit Function
        End If
        Set ExcuteSQL = rs
    End Function
    Private Sub Command1_Click()
    Dim sql As String
    sql = "select id,mc from mz"
     Set DataGrid1.DataSource = ExcuteSQL(sql)
     DataGrid1.Refresh
    End Sub