是"创建表工作表2为(从工作表1中选择*)"仅使用Excel ODBC / ADO驱动程序从Excel环境中支持SQL查询?

时间:2015-06-15 13:23:37

标签: sql excel vba

只想通过提交SQL查询(来自Excel VBA)从另一个创建一个Excel 2010工作表,例如,无需对记录集进行后续VBA。无法找到微软的工作示例或文档,这是不可能的,谢谢!

1 个答案:

答案 0 :(得分:0)

使用此:

ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add( _
      SourceType:=0, _
      Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Password=xxxxx;Persist Security Info=True;User ID=xxxxx;Data Source=yourServerNameOrIP;Use Procedure for Prepare=1;" _
        , "Auto Translate=True;Packet Size=4096;Workstation ID=yourPcName;Use Encryption for Data=False;" _
        , "Tag with column collation when possible=False;Initial Catalog=yourDbName") _
        , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdTable
    .CommandText = Array("""yourDbName"".""dbo"".""yourTableName""")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .SourceConnectionFile = "C:\xxxxxxxxx.odc"
    .ListObject.DisplayName = "Table__._yourDbName_yourTableName"
    .Refresh BackgroundQuery:=False
End With

当我录制宏时。