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] & "*"));
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.