我在MS Access中通过VBA代码&基本上具有相同的查询。只是一个保存的查询。它基本上是相同的查询,但是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;"
表单正在使用公司的输入字段,Start&amp;结束日期,&amp; ProdCodes。 ProdCodes位于表格的列表框中,其中包括ProdCode,Desc和&amp;公司(在VBA方面的表中)。这个VBA代码返回我需要的所有记录,除了2个丢失的记录。无论我是否加入上面的ProdCode VBA表,使用WHERE ID.ProdCode IN(选择不同的......)等在WHERE中明确选择它。
想法?在此先感谢!!!
答案 0 :(得分:1)
您的WHERE
子句不相同(因为两个查询之间的括号不在同一位置)。我怀疑你可能在条款的ID.PartNum<>''
部分遇到问题。
您可以尝试在VBA代码中设置断点并获取VBA查询的实际解释输出(包括参数值)。将该SQL语句复制到新的查询窗口(SQL视图),然后查看设计器以查看Access如何解释该语句。
您还可以尝试匹配VBA查询中的where子句以匹配参数的相同顺序和括号的数量,但是可能更容易使用设计者首先证明\反驳我的假设,然后再讨论VBA查询。
通常会遇到类似这样的问题,将陈述删除并逐渐添加回来以找到问题发生的确切位置是一个反复试验的问题,但括号用法是我开始调查的地方。