1

我有一个查询和一个表。表的名称是 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] & "*"));
4

2 回答 2

2

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

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.

如果这种方法不实用或难以理解,您可以使用 VBA 代码WHERE在运行时根据包含值的那些文本框来构造子句。

于 2013-10-24T14:01:20.080 回答
0

实际上有更好的方法可以做到这一点,但它更多的是中级知识水平(不确定你是否在那里)。您实际上应该循环遍历您的控件并检查每个控件的 NULL 值,您最终会得到一个 WHERE 子句,该子句中只有实际具有值的控件。上面的问题是,当你有一个 NULL 时,它是在说

((MyField) LIKE "*NULL*")

并且由于它在 AND 语句中,因此它将跳过该行。你将需要这样的东西:

Dim sSQL as String
Dim ctl As Control

sSQL = "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 ("
For Each ctl In frm
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox _
            Or ctl.ControlType = acListBox Then
            If Len(Trim(ctl.Value)) > 0 Then
                sSQL = sSQL & "((OrderT." & ctl.Name & ") Like "*" & [Forms]![SearchF]![" & ctl.Name & "] & "*") AND "
            End If
        End If
Next ctl

sSQL = Left(sSQL, Len(sSQL)-4)
sSQL = sSQL & ");"

DoCmd.RunSQL sSQL
于 2013-10-24T14:09:11.257 回答