2014-12-01 56 views
2

當我運行下面的代碼我接收到錯誤,「運行時錯誤‘1004’:應用程序定義或對象定義的錯誤」如何插入到一個臨時表中VBA

當我選擇調試下面一行被突出顯示:.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

您的查詢字符串不返回數據。將您的SQL更改爲SELECT語句而不是SELECT INTO(刪除INTO子句)。

+0

感謝您的答覆,但我想要運行的查詢到一個臨時表 – Adam 2014-12-02 00:36:17

+1

一個的QueryTable需要的數據;如果將INSERT INTO臨時表中,該表將在您的SQL數據庫中創建,而不是返回給Excel。 – 2014-12-02 15:50:11