2017-03-28 105 views
1

我使用VBA運行帶有Currentdb.Execute命令的INSERT INTO ... SELECT ... SQL查詢。但是,我的結果有問題。有一些行缺少數據。MS Access currentdb.excute與Docmd.RunSQL的結果不同

當使用Docmd.RunSQL和相同的SQL時,結果是正確的,但我不希望將警告消息插入表中。

我也試過只使用SELECT ...和Currentdb.Execute並在調試窗口上打印結果。結果是正確的,沒有丟失的數據。

這裏是我的代碼:

strsql = strsql & "INSERT INTO tempLineItems (OrderNo, PositionNo, PartNo, [Description], PlannedDeliveryDate, Qty, Unit, Price, Curr, txta, LineItemText, Discount, Tax) " 
strsql = strsql & "SELECT dbo_ttdsls041600.t_orno, dbo_ttdsls041600.t_pono, dbo_ttdsls041600.t_item, IIF(dbo_ttipcs021600.t_dsca is null,dbo_ttiitm001600.t_dsca,dbo_ttipcs021600.t_dsca) AS t_dsca, dbo_ttdsls041600.t_ddta, dbo_ttdsls041600.t_oqua, dbo_ttdsls041600.t_cups, dbo_ttdsls041600.t_pric, dbo_ttdsls040600.t_ccur, dbo_ttdsls041600.t_txta, Null AS LineItemText, dbo_ttdsls041600.t_disc_1, dbo_ttdsls041600.t_cvat " 
strsql = strsql & "FROM dbo_ttdsls040600 INNER JOIN ((dbo_ttipcs021600 RIGHT JOIN dbo_ttdsls041600 ON (dbo_ttipcs021600.t_item = dbo_ttdsls041600.t_item) AND (dbo_ttipcs021600.t_cprj = dbo_ttdsls041600.t_cprj)) LEFT JOIN dbo_ttiitm001600 ON dbo_ttdsls041600.t_item = dbo_ttiitm001600.t_item) ON dbo_ttdsls040600.t_orno = dbo_ttdsls041600.t_orno " 
strsql = strsql & "WHERE (((dbo_ttdsls041600.t_orno)=" & Me.txtSalesOrderNo.Value & ") AND ((dbo_ttdsls041600.t_pono)>0)) " 
strsql = strsql & "ORDER BY dbo_ttdsls041600.t_pono;" 

的問題是在

IIF(dbo_ttipcs021600.t_dsca is null,dbo_ttiitm001600.t_dsca,dbo_ttipcs021600.t_dsca) AS t_dsca 

我使用ISNULL()函數,它具有相同的結果作爲使用IS NULL

3/28/2017更新: 這些表是來自SQL Server的鏈接表。我根據鏈接數據的子集數據創建了本地表,並且根本沒有丟失數據。

+0

或者只是使用'NZ(dbo_ttipcs021600.t_dsca,dbo_ttiitm001600.t_dsca)'並保存一些按鍵。 – geeFlo

+0

我嘗試過NZ()函數,但結果與丟失的數據一樣。 – Hulapig

+0

爲什麼你打擾別名t_item,特別是用它自己的名字?記錄已插入,但缺少說明字段中的數據?任何其他?想要提供用於測試的示例數據?我推薦Box.com fileshare網站。 – June7

回答

0

很奇怪。但是我從來不需要用SQLServer表工作太多。要使用RunSQL並且不會收到警告,請將它們關閉再打開。

DoCmd.SetWarnings False 
DoCmd.RunSQL ... 
DoCmd.SetWarnings True 
+0

謝謝。這工作。 – Hulapig

0

我只是好奇這個問題,所以我通過使用多層查詢做了一個不同的方法。

strsql = strsql & "INSERT INTO tempLineItems (OrderNo, PositionNo, PartNo, Description, PlannedDeliveryDate, Qty, Unit, Price, Curr, txta, LineItemText, Discount, Tax) " 
strsql = strsql & "SELECT SO1.t_orno, SO1.t_pono, SO1.t_item, nz(SO1.t_dsca, dbo_ttiitm001600.t_dsca) AS t_dsca, SO1.t_ddta, SO1.t_oqua, SO1.t_cups, SO1.t_pric, SO1.t_ccur, SO1.t_txta, Null AS LineItemText, SO1.t_disc_1, SO1.t_cvat " 
strsql = strsql & "FROM (SELECT SO.t_orno, SO.t_ccur, SO.t_pono, SO.t_item, SO.t_oqua, SO.t_pric, SO.t_cups, SO.t_ddta, SO.t_txta, SO.t_disc_1, SO.t_cvat, dbo_ttipcs021600.t_dsca " 
strsql = strsql & "FROM (SELECT dbo_ttdsls040600.t_orno, dbo_ttdsls040600.t_ccur, dbo_ttdsls041600.t_pono, dbo_ttdsls041600.t_item, dbo_ttdsls041600.t_pric, dbo_ttdsls041600.t_cups, dbo_ttdsls041600.t_ddta, dbo_ttdsls041600.t_cprj, dbo_ttdsls041600.t_txta, dbo_ttdsls041600.t_disc_1, dbo_ttdsls041600.t_cvat, dbo_ttdsls041600.t_oqua " 
strsql = strsql & "FROM dbo_ttdsls041600 INNER JOIN dbo_ttdsls040600 ON dbo_ttdsls041600.t_orno = dbo_ttdsls040600.t_orno " 
strsql = strsql & "WHERE (((dbo_ttdsls040600.t_orno)=" & Me.txtSalesOrderNo & "))) SO " 
strsql = strsql & "LEFT JOIN dbo_ttipcs021600 ON dbo_ttipcs021600.t_cprj = SO.t_cprj AND dbo_ttipcs021600.t_item = SO.t_item) AS SO1 LEFT JOIN dbo_ttiitm001600 ON dbo_ttiitm001600.t_item = SO1.t_item " 
strsql = strsql & "ORDER BY SO1.t_pono;" 

這個工程!我想在內部存在一些問題。

相關問題