【发布时间】:2017-07-20 18:18:48
【问题描述】:
有没有办法以更有效的方式设置此代码?我试图过滤几个不同的标准,我什至很难编译代码。此外,所有 IS NULL 的原因是我想这样做,以便在没有输入信息的情况下,它只接受所有数据。
有人对如何优化此代码有任何提示吗?特别是 WHERE 部分。有没有我可以使用的 if then 语句?还是索引?
SELECT [Table Material Label].Serial, [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier,
[Table Material Label].[Lot Number], [Table Material Label].Weight, [Table Material Label].Quantity, [Table Material Label].[Purchase Order Number], [Table Material Label].[Received By], [Table Material Label].[Checked in By], [Table Material Label].[Total Weight]
FROM [Table Material Label]
WHERE (([Table Material Label].[Date Recieved])>=[Forms]![Report Generator]![Text6]
AND ([Table Material Label].[Date Recieved])<=[Forms]![Report Generator]![Text7])
AND ([Table Material Label].MaterialDescription = [Forms]![Report Generator]![repMaterial]
OR [Forms]![Report Generator]![repMaterial] IS NULL)
AND ([Table Material Label].MaterialCode = [Forms]![Report Generator]![repItem]
OR [Forms]![Report Generator]![repItem] IS NULL)
AND ([Table Material Label].[Lot Number] = [Forms]![Report Generator]![repLot]
OR [Forms]![Report Generator]![repLot] IS NULL)
AND ([Table Material Label].Weight = [Forms]![Report Generator]![repWeight]
OR [Forms]![Report Generator]![repWeight] IS NULL)
AND ([Table Material Label].Quantity = [Forms]![Report Generator]![repQuantity]
OR [Forms]![Report Generator]![RepQuantity] IS NULL)
AND ([Table Material Label].[Purchase Order Number] = [Forms]![Report Generator]![repPurchaseOrder]
OR [Forms]![Report Generator]![repPurchaseOrder] IS NULL)
AND ([Table Material Label].[Received By] = [Forms]![Report Generator]![repRecBy]
OR [Forms]![Report Generator]![repRecBy] IS NULL)
AND ([Table Material Label].[Checked in By] = [Forms]![Report Generator]![repCheckBy]
OR [Forms]![Report Generator]![repCheckBy] IS NULL)
AND ([Table Material Label].[Total Weight] = [Forms]![Report Generator]![repTotalWeight]
OR [Forms]![Report Generator]![repTotalWeight] IS NULL)
AND ([Table Material Label].Supplier = [Forms]![Report Generator]![Supp]
OR [Forms]![Report Generator]![Supp] IS NULL)
ORDER BY [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier;
【问题讨论】:
-
对于所有 or 的考虑:
AND ([Table Material Label].MaterialDescription = Nz([Forms]![Report Generator]![repMaterial], [Table Material Label].MaterialDescription)从而消除所有 ors...不确定它是否有助于不进行测试。如果形式 repMaterial 为空,则基本使用材料描述代替,本质上 1=1 并且可以省略 or。 -
xQbert,这非常好用。谢谢。更新主帖。