PRB: Cannot Find NULL Records with Parameterized Query
Article ID: 168207
Article Last Modified on 1/8/2003
APPLIES TO
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q168207
SYMPTOMS
Running a parameterized query against a Jet database where the parameter
variable is equal to NULL will not return any records. Running a query
without the parameter variable that specifically searches for NULL will
succeed as expected.
CAUSE
A variable with a NULL value sent in as the parameter is not equal to what
the JET engine recognizes as NULL. The values of the NULLs do not match.
RESOLUTION
Before calling the code that executes the query, use an IF statement that
checks the value of the parameter. If the value is NULL, run a different
query that specifically looks for a NULL value, otherwise run the original,
parameterized query.
Example:
Dim p1 as long
dim r1 as recordset
dim SQLstring as string
dim p1 as long
if p1 = NULL then
SQLstring = "SELECT Table1.Field1 FROM Table1 _
WHERE (((Table1.Field1) Is Null));"
'Looking specifically for a Null
else
SQLstring = "SELECT Table1.Field1 FROM Table1 _
WHERE (((Table1.Field1)=[" & p1 & "]));"
'Parameterized query
end if
set r1 = MyDatabase.openrecordset(SQLstring, dbOpenDynaset)
STATUS
This behavior is by design.
Additional query words: kbVBp500 kbVBp600 kbdse kbDSupport kbVBp kbNoKeyWord
Keywords: kbprb KB168207