Rst 是个 记录条 SQL是个字符穿变量,放着SQL语句,CNN是个connection
这个是ADO的一个问题,请高手给我指个方向,原模块在下面,参考一下,调试成功立即给分
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.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))) ThenCnn.Execute SQL '执行查询语句
MsgString = sTokens(0) & "查询成功"
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_ExitEnd Function

解决方案 »

  1.   

    sTokens = Split(SQL) ?看看 Trim$(SQL) 是什么?
      

  2.   

    Public Function chaxun(ByVal sql As String, msgstring As String) As ADODB.Recordset
        On Error GoTo ExecuteSQL_Error
        
        Dim cN As ADODB.Connection
        Dim rst As ADODB.Recordset
        Dim a() As String
        Set cN = New ADODB.Connection
        
        
        Dim str As String
        str = App.Path
        If Right(str, 1) <> "\" Then
           str = str + "\"
        End If    cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\_data.mdb;Persist Security Info=False;jet OLEDB:Database password=ljhyyuuiui123"
        cN.Open
        
        
        a = Split(sql)
        
        If InStr("insert,delete,updata,alter", Trim(a(0))) Then
            cN.Execute sql
            Select Case Trim(a(0))
                Case "insert"
                    msgstring = "添加记录"
                Case "delete"
                    msgstring = "删除记录"
                Case "updata"
                    msgstring = "更新记录"
            End Select
            msgstring = msgstring & "已完成"
        Else
            
            Set rst = New ADODB.Recordset
            rst.CursorLocation = adUseClient
            rst.Open sql, cN, adOpenKeyset, adLockOptimistic
            
            Set chaxun = rst
            msgstring = "查询到" & rst.RecordCount & "条记录"
        End If
        
    ExecuteSQL_Exit:
        Set rst = Nothing
        Set cN = Nothing
        Exit Function
        
    ExecuteSQL_Error:
        msgstring = "查询错误: " & _
        err.Description
        Resume ExecuteSQL_Exit
    End Function
      

  3.   

    楼上的朋友,我刚试了你程序,你的“
        cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\_data.mdb;Persist Security Info=False;jet OLEDB:Database password=ljhyyuuiui123"
    这句话,打开的是什么类型的数据库?我用的是ACCESS不知道,你这条语句在打开ACCESS的时候好象有点困难
      

  4.   

    我的QQ是81256592,大家在QQ里教我也可以
      

  5.   

    楼主,不会呀! hlm750908(pro dog) 的连接字符串没有问题,我写的Access数据库程序也是用这样的连接,并没有什么问题。题外话,你也喜欢霸王丸吗?
      

  6.   

    改成:
    Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.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
    Set Rst = New ADODB.Recordset   '创建数据对象If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
        Cnn.Execute SQL '执行查询语句
        Set ExecuteSQL = New ADODB.Recordset
        MsgString = sTokens(0) & "查询成功"
    Else
        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
      

  7.   

    改为:Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.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
    Set ExecuteSQL = New ADODB.Recordset
    If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) ThenCnn.Execute SQL '执行查询语句
    MsgString = sTokens(0) & "查询成功"
    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_ExitEnd Function