Reason all records return is the empty form controls return any value as you are concatenating nothing with the wildcards so returns this search pattern ** in the LIKE evaluation. As shown below with empty strings or nulls, this search pattern returns all records that have any non-empty character.
SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & '' & "*")
AND (FieldB Like "*" & '' & "*") AND ...;
SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & NULL & "*")
AND (FieldB Like "*" & NULL & "*") AND ...;
To resolve, consider concatenating a character(s) that would not show up in you columns with the NZ() replacement. Below logic now will return all records by form search values as is. However, if form values are blank, NZ() substitutes with ~ and any records with this character in column will return. Assuming no tilda ever exists in that column, no records output.
SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & NZ([Forms]![frmA]![FieldA], '~') & "*")
AND (FieldB Like "*" & NZ([Forms]![frmA]![FieldB], '~') AND ...;
Turns out for full implementation the NZ() substitution value must be replaced dynamically. Hence, consider incorporating VBA per mutually exclusive conditions:
When all fields are empty, NZ() should use tilda (~).
When at least one field is non-empty, all remaining empty fields' NZ() should use the wildcard operator, asterisk (*).
Dim i As Integer, num_search_fields As Integer
Dim strSQL As String
Dim var As Variant
i = 0
num_search_fields = 5
' LOOP THROUGH ALL SEARCH FIELDS
For Each var in Array("FieldA", "FieldB", "FieldC", "FieldD", "FieldE")
If IsNull(Forms("frmA").Controls(var).Value) Then
i = i + 1
End If
Next var
' CONDITIONALLY BUILD SQL
If i = num_search_fields Then
strSQL = "SELECT FieldA, FieldB, FieldC, FieldD, FieldE" _
& " FROM tblA" _
& " WHERE (FieldA Like '*' & NZ([Forms]![frmA]![FieldA], '~') & '*')" _
& " AND (FieldB Like '*' & NZ([Forms]![frmA]![FieldB], '~') & '*')" _
& " AND (FieldC Like '*' & NZ([Forms]![frmA]![FieldC], '~') & '*')" _
& " AND (FieldD Like '*' & NZ([Forms]![frmA]![FieldD], '~') & '*')" _
& " AND (FieldE Like '*' & NZ([Forms]![frmA]![FieldE], '~') & '*')"
Else
strSQL = "SELECT FieldA, FieldB, FieldC, FieldD, FieldE" _
& " FROM tblA" _
& " WHERE (FieldA Like '*' & NZ([Forms]![frmA]![FieldA], '*') & '*')" _
& " AND (FieldB Like '*' & NZ([Forms]![frmA]![FieldB], '*') & '*')" _
& " AND (FieldC Like '*' & NZ([Forms]![frmA]![FieldC], '*') & '*')" _
& " AND (FieldD Like '*' & NZ([Forms]![frmA]![FieldD], '*') & '*')" _
& " AND (FieldE Like '*' & NZ([Forms]![frmA]![FieldE], '*') & '*')"
End If
' ASSIGN SQL STRING TO SUBFORM RECORDSOURCE
Forms!frmA!subform.Form.RecordSource = strSQL
Forms!frmA!subform.Form.Requery