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))) Then
      cnn.Execute SQL
      MsgString = sTokens(0) & " query successful"
   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_Exit
End Function

解决方案 »

  1.   

    Public Function ExecuteSQL(ByVal SQL As String)As ADODB.Recordset   Dim cnn As ADODB.Connection
       Dim rst As ADODB.Recordset
       Dim MsgString As String
       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"
          msgbox MsgString
       Else
          Set rst = New ADODB.Recordset
          rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic      Set ExecuteSQL = rst
          MsgString = "查询到" & rst.RecordCount & " 条记录 "
          Set rst = Nothing
          msgbox MsgString
       End If
       Set cnn = Nothing
    ExecuteSQL_Exit:
       Set rst = Nothing
       Set cnn = Nothing
       Exit Function
       
    ExecuteSQL_Error:
       MsgString = "查询错误: " & Err.Description
       Resume ExecuteSQL_Exit
    End Function