Question

I have a query and a table. The table's name is OrderT, while the query's name is SearchQ. The query is there so I can have a subform show the results of a search on a form. Though my query won't show a line on the table if it's missing any data from any of the fields. For example, if I have the fields CustomerName, OrderNumber, and OrderDueDate and I fill out CustomerName, OrderNumber but leave OrderDueDate empty; the query won't show that it exists so the search can't pick it up.

How would I go about in making the query show all things on the table, even if some fields aren't filled out on them?

SQL of Query:

    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] & "*"));
Was it helpful?

Solution

Create a simpler test case and work out the logic there.

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
    );

When a value is entered in the CustomerName text box, that query will return only rows where the CustomerName field includes that text box value. And when no value is entered in the text box, the query does not exclude any rows based on the CustomerName field values. (I find it easier to keep this straight by giving the text box a different name than the field: txtCustomerName; and CustomerName.)

Same for OrderName.

If this approach is unwieldy or hard to understand, you could use VBA code to construct the WHERE clause at run time based on those text boxes which include a value.

OTHER TIPS

There's actually a better way to do this, but it's more of an Intermediate knowledge level (not sure if you're there or not). You should actually loop through your controls and check each one for NULL values, and you'd end up with a WHERE clause that only has controls that actually have values in them. The problem above is that when you have a NULL, it's saying

((MyField) LIKE "*NULL*")

and since it's in an AND statement, it's going to skip that line. You're going to need something like this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top