AAA said:
Hello and thanks,
I'm afraid you misunderstood my question:
The NULL values are not in the DB, but in the form!
For instance, say I have a FirstName and LastName form fields.
You can search for "George" as first-name and "Bush" as last-name.
BUT, if the user only fills-in the FirstName with "George",
I want to retrieve all "George" records, regardless of LastName.
Currently, I get exceptions because the WHERE clause contains
parts that refer to the LastName form field, which is left blank (null).
Thanks
Meir
Ah, now I understand. If you're using a static, stored query, you can set
it up like this:
SELECT * FROM MyTable WHERE
((FirstName = [Forms]![frmMyForm]![txtFirstName])
OR ([Forms]![frmMyForm]![txtFirstName] Is Null))
AND
((LastName = [Forms]![frmMyForm]![txtLastName])
OR ([Forms]![frmMyForm]![txtLastName] Is Null))
If you're building the query on the fly in VBA code, you can simply check
which form controls are supplied and build the SQL statement accordingly:
'----- start of example code -----
Dim strSQL As String
Const Q As String = """"
Constr QQ As String = Q & Q
If Not IsNull(Me!txtFirstName) Then
strSQL = strSQL " AND (FirstName = " & _
Q & Replace(Me!FirstName, Q, QQ) & Q & ")"
End If
If Not IsNull(Me!txtLastName) Then
strSQL = strSQL " AND (LastName = " & _
Q & Replace(Me!LastName, Q, QQ) & Q & ")"
End If
' If any criteria were specified, convert to a WHERE clause
' and drop leading " AND ".
If Len(strSQL) > 0 Then
strSQL = " WHERE " & Mid(StrSQL, 6)
End If
strSQL = "SELECT * FROM MyTable" & strSQL
'----- end of example code -----