0

我通过 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 中明确选择它,都不起作用。

想法?提前致谢!!!

4

1 回答 1

1

您的WHERE子句不相同(因为括号在两个查询之间的位置不同)。我怀疑您可能ID.PartNum<>''对条款的部分有问题。

您可以尝试在 VBA 代码中设置断点并获取 VBA 查询的实际解释输出(包括参数值)。将该 SQL 语句复制到新的查询窗口(SQL 视图)中,然后查看设计器以了解 Access 如何解释该语句。

您还可以尝试匹配 VBA 查询中的 where 子句以匹配相同的参数顺序和括号数量,但在使用 VBA 查询之前,首先使用设计器来证明\反驳我的假设可能更容易。

通常遇到此类问题,需要反复试验才能将语句拿走并逐渐将它们添加回来以找出问题发生的确切位置,但括号的用法是我开始调查的地方。

于 2013-02-27T20:14:41.423 回答