小编典典

查询以显示表格,即使未输入某些字段

sql

我有一个查询和一个表。该表的名称为OrderT,而查询的名称为SearchQ。查询在那里,所以我可以有一个子表单来显示表单上的搜索结果。尽管我的查询如果缺少任何字段中的任何数据,也不会在表上显示一行。例如,如果我有CustomerName,OrderNumber和OrderDueDate字段,而我填写了CustomerName,OrderNumber,但将OrderDueDate留为空白;该查询不会显示该查询是否存在,因此搜索无法找到该查询。

即使没有填写某些字段,如何使查询显示表中的所有内容呢?

SQL查询:

    SELECT OrderT.CustomerName, OrderT.OrderName, OrderT.OrderDesc, OrderT.DateOfPurchase, OrderT.ProjectDueDate, OrderT.EngineerDueDate, OrderT.ProjectComplete, OrderT.CutplanDueDate, OrderT.MaterialSpecs, OrderT.CutplanCode, OrderT.HardwareSpecs, OrderT.HardwareDueDate, OrderT.HardwareComplete, OrderT.PurchaseOrder, OrderT.PurchaseSupplier
FROM OrderT
WHERE (((OrderT.CustomerName) Like "*" & [Forms]![SearchF]![CustomerName] & "*") AND ((OrderT.OrderName) Like "*" & [Forms]![SearchF]![OrderName] & "*") AND ((OrderT.OrderDesc) Like "*" & [Forms]![SearchF]![OrderDesc] & "*") AND ((OrderT.DateOfPurchase) Like "*" & [Forms]![SearchF]![DateOfPurchase] & "*") AND ((OrderT.ProjectDueDate) Like "*" & [Forms]![SearchF]![ProjectDueDate] & "*") AND ((OrderT.EngineerDueDate) Like "*" & [Forms]![SearchF]![EngineerDueDate] & "*") AND ((OrderT.ProjectComplete) Like "*" & [Forms]![SearchF]![ProjectComplete] & "*") AND ((OrderT.CutplanDueDate) Like "*" & [Forms]![SearchF]![CutplanDueDate] & "*") AND ((OrderT.MaterialSpecs) Like "*" & [Forms]![SearchF]![MaterialSpecs] & "*") AND ((OrderT.CutplanCode) Like "*" & [Forms]![SearchF]![CutplanCode] & "*") AND ((OrderT.HardwareSpecs) Like "*" & [Forms]![SearchF]![HardwareSpecs] & "*") AND ((OrderT.HardwareDueDate) Like "*" & [Forms]![SearchF]![HardwareDueDate] & "*") AND ((OrderT.HardwareComplete) Like "*" & [Forms]![SearchF]![HardwareComplete] & "*") AND ((OrderT.PurchaseOrder) Like "*" & [Forms]![SearchF]![PurchaseOrder] & "*") AND ((OrderT.PurchaseSupplier) Like "*" & [Forms]![SearchF]![PurchaseSupplier] & "*"));

阅读 184

收藏
2021-04-14

共1个答案

小编典典

创建一个更简单的测试用例,并在那里计算逻辑。

SELECT
    o.CustomerName,
    o.OrderName
FROM OrderT AS o
WHERE
    (
            o.CustomerName Like "*" & [Forms]![SearchF]![CustomerName] & "*"
        OR [Forms]![SearchF]![CustomerName] Is Null
    )
    AND
    (
            o.OrderName Like "*" & [Forms]![SearchF]![OrderName] & "*"
        OR [Forms]![SearchF]![OrderName] Is Null
    );

CustomerName文本框中输入一个值时,该查询将仅返回该CustomerName字段包含该文本框值的行。并且在文本框中未输入任何值时,查询不会基于CustomerName字段值排除任何行。(我觉得更容易通过赋予文本框比外地不同的名称,以保持这个直:txtCustomerName;和CustomerName。)

相同OrderName

如果这种方法笨拙或难以理解,则可以WHERE在运行时使用VBA代码基于包含值的那些文本框来构造该子句。

2021-04-14