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.