我正在将Excel电子表格中的数据导入tblPOLinesTemp
表,然后我必须将这些数据附加到tblPOLines
表。我试图从VBA执行insert into
SQL语句,但继续得到"查询输入必须包含至少一个表或查询"作为错误,我似乎无法弄清楚在Access中运行它时SQL语句是否有效!这让我抓狂!你能帮忙吗?
我的代码(我有Dim' ed AppendPOlines as String):
AppendPOLines = "INSERT INTO tblPOlines ( PurchaseOrderID, PurchaseOrderLineID, [Material#], FirstDeliveryDate, Quantity, CreatedBy )" _
& "SELECT tblPOLinesTemp.PurchaseOrderID, tblPOLinesTemp.PurchaseOrderLineID, tblPOLinesTemp.[Material#], tblPOLinesTemp.FirstDeliveryDate, tblPOLinesTemp.Quantity, [TempVars]![VarUserID] AS CreatedBy" _
& "FROM tblPOLinesTemp LEFT JOIN tblPOlines ON (tblPOLinesTemp.PurchaseOrderLineID = tblPOlines.PurchaseOrderLineID) AND (tblPOLinesTemp.[PurchaseOrderID] = tblPOlines.[PurchaseOrderID])" _
& "WHERE (((tblPOlines.PurchaseOrderLineID) Is Null) AND ((tblPOlines.PurchaseOrderID) Is Null));"
CurrentDb.Execute AppendPOLines, dbFailOnError
请注意,左连接是为了确保Query不会插入具有类似采购订单和采购订单行ID的行 - 以避免重复行。
谢谢!
答案 0 :(得分:1)
每个[“]之前和之后放一个空格:
AppendPOLines = " INSERT INTO tblPOlines( PurchaseOrderID, PurchaseOrderLineID, [Material#], FirstDeliveryDate, Quantity, CreatedBy ) " _
& " SELECT tblPOLinesTemp.PurchaseOrderID, tblPOLinesTemp.PurchaseOrderLineID, tblPOLinesTemp.[Material#], tblPOLinesTemp.FirstDeliveryDate, tblPOLinesTemp.Quantity, [TempVars]![VarUserID] AS CreatedBy " _
& " FROM tblPOLinesTemp LEFT JOIN tblPOlines ON (tblPOLinesTemp.PurchaseOrderLineID = tblPOlines.PurchaseOrderLineID) AND (tblPOLinesTemp.[PurchaseOrderID] = tblPOlines.[PurchaseOrderID]) " _
& " WHERE (((tblPOlines.PurchaseOrderLineID) Is Null) AND ((tblPOlines.PurchaseOrderID) Is Null)); "
您的查询执行如下:
INSERT INTO tblPOlines(PurchaseOrderID,PurchaseOrderLineID,[Material#],FirstDeliveryDate,Quantity,CreatedBy )SELECT tblPOLinesTemp.PurchaseOrderID,tblPOLinesTemp.PurchaseOrderLineID,tblPOLinesTemp。[Material#],tblPOLinesTemp.FirstDeliveryDate,tblPOLinesTemp .Quantity,[TempVars]![VarUserID] AS CreatedByFROM tblPOLinesTemp LEFT JOIN tblPOlines ON(tblPOLinesTemp.PurchaseOrderLineID = tblPOlines.PurchaseOrderLineID)AND(tblPOLinesTemp。[PurchaseOrderID] = tblPOlines。[PurchaseOrderID] ) WHERE (((tblPOlines.PurchaseOrderLineID)为空)AND((tblPOlines.PurchaseOrderID)为空));
所以CreatedByFrom是错的,你得到这个错误: “查询输入必须包含至少一个表或查询”