Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
   '传递参数：SQL 传递查询语句，MsgString传递查询信息，返回执行结果记录集 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.BeginTrans
      cnn.Execute SQL
      cnn.CommitTrans
      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_Exit
End Function

