我基本上在MS Access中通過VBA代碼&查詢只是一個保存的查詢。這基本上是相同的查詢,但VBA代碼缺少兩個記錄,應該包含符合條件的&。保存的查詢返回相同的數據集和VBA未返回的兩個「缺失」記錄。MS Access窗體(VBA)查詢和定期查詢給出不同的結果
保存的查詢SQL:
SELECT ID.Company, CU.GroupCode, CU.CustID, CU.Name, ST.ShipToNum, ST.Name, IH.InvoiceDate, IH.InvoiceNum, ID.ProdCode, ID.PartNum
FROM ((((PUB_InvcDtl AS ID INNER JOIN PUB_InvcHead AS IH ON (ID.CustNum = IH.CustNum) AND (ID.InvoiceNum = IH.InvoiceNum) AND (ID.Company = IH.Company)) LEFT JOIN PUB_Part AS PT ON (ID.Company = PT.Company) AND (ID.PartNum = PT.PartNum)) LEFT JOIN PUB_ShipTo AS ST ON (ID.CustNum = ST.CustNum) AND (ID.ShipToNum = ST.ShipToNum) AND (ID.Company = ST.Company)) LEFT JOIN V_Customer AS CU ON (ID.Company = CU.Company) AND (ID.CustNum = CU.CustNum)) LEFT JOIN ProdCodeCommRate AS PCC ON ID.ProdCode = PCC.ProdCode
WHERE (((ID.Company)="BTN") AND ((IH.InvoiceDate) Between #1/1/2013# And #1/31/2013#) AND ((ID.ProdCode) In ('2100','2110','2200','2210','2300','2400','2405','2500','2600','2701','2702','2709','2710','2800','2901','2902','2903')) AND ((ID.PartNum)<>''));
VBA查詢:
"SELECT ID.Company, CU.GroupCode, CU.CustID, CU.Name AS CustName, ST.ShipToNum, ST.Name AS ShipToName, IH.InvoiceDate, IH.InvoiceNum, ID.InvoiceLine, ID.PartNum, " & _
"ID.ProdCode, PCC.CommRate, ID.PricePerCode, PT.PartDescription, ID.IUM, PT.CostMethod, ID.UnitPrice, ID.OurShipQty, ID.ExtPrice, ID.Discount, [ExtPrice]-[Discount] AS NetPrice, " & _
"ID.LbrUnitCost, ID.BurUnitCost, ID.MtlUnitCost, ID.SubUnitCost, [LbrUnitCost]+[BurUnitCost]+[MtlUnitCost]+[SubUnitCost] AS TotUnitCost, " & _
"([LbrUnitCost]+[BurUnitCost]+[MtlUnitCost]+[SubUnitCost])*[OurShipQty] AS ExtTotCost, IH.OpenInvoice, getSalesRep([IH].[SalesRepList],1,ID.Company) AS SalesRep1, ID.RepRate1, " & _
"ID.RepSplit1, getSalesRep([IH].[SalesRepList],2,ID.Company) AS SalesRep2, ID.RepRate2, ID.RepSplit2, getSalesRep([IH].[SalesRepList],3,ID.Company) AS SalesRep3, " & _
"ID.RepRate3, ID.RepSplit3 " & _
"FROM (((((PUB_InvcDtl AS ID " & _
"INNER JOIN PUB_InvcHead AS IH ON (ID.CustNum = IH.CustNum) AND (ID.InvoiceNum = IH.InvoiceNum) AND (ID.Company = IH.Company)) " & _
"LEFT JOIN PUB_Part AS PT ON (ID.Company = PT.Company) AND (ID.PartNum = PT.PartNum))" & _
"LEFT JOIN PUB_ShipTo AS ST ON (ID.CustNum = ST.CustNum) AND (ID.ShipToNum = ST.ShipToNum) AND (ID.Company = ST.Company))" & _
"LEFT JOIN V_Customer AS CU ON (ID.Company = CU.Company) AND (ID.CustNum = CU.CustNum))" & _
"LEFT JOIN ProdCodeCommRate AS PCC ON ID.ProdCode = PCC.ProdCode)" & _
"WHERE ((ID.Company)=[Forms]![frmSalesMgnAnalysis]![cboComp]) AND ((ID.PartNum)<>'') AND " & _
"((IH.InvoiceDate) Between [Forms]![frmSalesMgnAnalysis]![dtStart] And [Forms]![frmSalesMgnAnalysis]![dtEnd]) AND " & _
"((ID.ProdCode) IN ('2100','2110','2200','2210','2300','2400','2405','2500','2600','2701','2702','2709','2710','2800','2901','2902','2903'))" & _
"ORDER BY ID.ProdCode, IH.InvoiceDate, CU.Name;"
的形式是使用輸入域公司,開始&結束日期,& ProdCodes。 ProdCodes位於包含ProdCode,Desc,&公司(位於VBA一側的表格中)的表單中的列表框中。這個VBA代碼返回我需要的所有記錄,除了2條缺失的記錄外。無論我是否加入上面的ProdCode VBA表,在WHERE中使用WHERE ID.ProdCode IN(Select distinct ......)等選擇它都不會起作用。
想法?提前致謝!!!
如果您刪除對輸入字段的引用並將它們替換爲SQL查詢中的硬編碼值,會發生什麼情況?如果這返回正確的結果,那麼你知道問題是值通過你的輸入字段之一傳遞。 – 2013-02-27 18:31:25
我可以刪除引用,我仍然會獲得記錄集,只有沒有應該包含的兩條記錄。我也雙倍和三倍檢查日期/時間。冉獨立查詢,發現兩個缺失的記錄中沒有任何內容會使他們失去資格。 – user2116602 2013-02-27 18:54:57
對不起,無法上傳數據庫。它太大了,並且其中有敏感數據。我已經發布了關於我將能夠避免陷入困境的所有信息。我認爲這可能是一個形式本身的問題,所以我創建了一個新的表單,它仍然返回所有我想要的,只是它省略了這兩個記錄。巨大的再次感謝所有的幫助! – user2116602 2013-02-27 19:23:47