我通过 VBA 代码在 MS Access 中有基本相同的查询,只是一个保存的查询。它基本上是相同的查询,但是 VBA 代码缺少两条符合条件的记录 & 应该包括在内。保存的查询返回相同的数据集和 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;"
该表单使用公司、开始和结束日期以及产品代码的输入字段。ProdCode 位于包含 ProdCode、Desc 和 Company 的表单的列表框中(位于 VBA 端的表中)。此 VBA 代码返回我需要的所有记录,除了 2 个丢失的记录。无论我加入上面的 ProdCode VBA 表,使用 WHERE ID.ProdCode IN (Select distinct...) 等在 WHERE 中明确选择它,都不起作用。
想法?提前致谢!!!