如何在VBA中插入TEMP表

时间:2014-12-01 22:11:00

标签: vba excel-vba excel

当我运行以下代码时,我收到错误,“运行时错误'1004':应用程序定义或对象定义错误”

当我选择调试时,突出显示以下行:.Refresh BackgroundQuery:= False

querystr = "SET NOCOUNT ON" & Chr(13) & _
        "SELECT CSD.StoreNo AS 'StoreNo',SUM(CSD.Amount) as totalSales " & Chr(13) & _
        "INTO    #SalesOfTheStores " & Chr(13) & _
        "FROM Purchase.dbo.CashsheetDetail as CSD " & Chr(13) & _
        "INNER JOIN Purchase.dbo.CashsheetHeader as CSH on CSH.TransferID = CSD.TransferID and CSH.StoreNo = CSD.StoreNo " & Chr(13) & _
        "WHERE  CSD.Comments = 'Total Gross Sales' AND CSH.DayendDate between '" & StartDate & "' And '" & EndDate & "' " & Chr(13) & _
        "GROUP BY CSD.StoreNo; "



With ActiveSheet.QueryTables.Add(Connection:= _
    "ODBC;DRIVER=SQL Server;SERVER=" & Div & "DBL01\SR;UID=" & User & ";APP=Microsoft Office 2003;WSID=" & PC & ";DATABASE=Purchase;Trusted_Connection=Yes" _
    , Destination:=Range("A1"))
    .RefreshStyle = xlOverwriteCells
    .CommandText = querystr
    .Refresh BackgroundQuery:=False

结束

1 个答案:

答案 0 :(得分:0)

您的查询字符串不会返回数据。将SQL更改为SELECT语句而不是SELECT INTO(删除INTO子句)。