我使用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的鏈接表。我根據鏈接數據的子集數據創建了本地表,並且根本沒有丟失數據。
或者只是使用'NZ(dbo_ttipcs021600.t_dsca,dbo_ttiitm001600.t_dsca)'並保存一些按鍵。 – geeFlo
我嘗試過NZ()函數,但結果與丟失的數據一樣。 – Hulapig
爲什麼你打擾別名t_item,特別是用它自己的名字?記錄已插入,但缺少說明字段中的數據?任何其他?想要提供用於測試的示例數據?我推薦Box.com fileshare網站。 – June7