【问题标题】:Query to show table, even if some fields aren't entered查询以显示表格,即使某些字段未输入
【发布时间】:2013-10-24 13:31:26
【问题描述】:

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

【问题讨论】:

  • 请向我们展示查询 SQL。我从来没有听说过这样的事情,除非有一个 join 或 WHERE 子句排除基于给定字段的记录。
  • 字段的值未在表格或搜索表单中输入?
  • 假设我在我的表 OrderT 中输入了数据,并留下了 OrderName,因为没有此特定产品的订单号。查询将不接受整行,并且就好像它不存在一样,使基于查询的子表单也无法获取数据,因此当您使用表单通过我的搜索按钮搜索数据时;它不会找到它,因为它认为它不存在,但它显然在表中。

标签: sql ms-access jet


【解决方案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 也一样。

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

【讨论】:

  • 感谢您的帮助和快速回复!非常感谢,我欠你这个世界。我希望如果您将来需要帮助,您可以像帮助我一样快速获得帮助,这是您应得的!
【解决方案2】:

实际上有一个更好的方法可以做到这一点,但它更多的是中级知识水平(不确定你是否在那里)。您实际上应该遍历您的控件并检查每个控件的 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

【讨论】:

  • 当 HansUp 发布他的答案时,我正在发布这个庞然大物,他的答案可能更好,但我会留在这里以防万一。您确实应该限制某人可以搜索的字段,或者制作一个搜索表单,人们可以从下拉列表中选择 1 或 2 个条件,并且只搜索这些条件。
  • 我使用了 HansUp,它完美无瑕。我公开承认我绝对不是 SQL、Access 或任何真正在 Access 中的东西的中级知识水平。虽然我知道初学者的理解,但我已经完成了我正在处理的 99% 的数据库!感谢您的尝试,并启动了快速响应。你不明白我对你和汉斯普的感激之情!很多爱。
猜你喜欢
  • 1970-01-01
  • 2019-09-25
  • 2020-08-12
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多