Microsoft Knowledge Base Email Alertz

(237992) - When using an SQL statement with a WHERE clause that contains a test for NULL using the = operator, records that match the query are not returned in the recordset. This happens with Jet 4.0 when using DAO, ODBC or OLE DB. Here are a few examples of...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 237992 - Last Review: December 5, 2003 - Revision: 4.1

PRB: Query Testing for NULL in Access Database Does not Return Records with Jet 4.0

This article was previously published under Q237992

On This Page

SYMPTOMS

When using an SQL statement with a WHERE clause that contains a test for NULL using the = operator, records that match the query are not returned in the recordset. This happens with Jet 4.0 when using DAO, ODBC or OLE DB. Here are a few examples of such queries:
SELECT * FROM Customers WHERE region = NULL
SELECT * FROM Customers WHERE region = NOT NULL
				
These queries worked correctly with Jet 3.5 and returned the records as expected.
This behavior will only be exhibited on computers with MDAC 2.1 and later that have Jet 4.0 installed.

CAUSE

Jet 4.0 made some changes to the SQL syntax and is now more SQL ANSI 92 compliant than before. Especially SQL ANSI 92 NULL behavior was implemented. Also remember that the result of a Boolean expression can have three results - TRUE, FALSE and NULL (in some documents referred as UNKNOWN).

The ANSI compliant syntax for testing NULL values uses the IS keyword. Examples of queries that work are:
SELECT * FROM Customers WHERE region IS NULL
SELECT * FROM Customers WHERE region IS NOT NULL
				

RESOLUTION

Use the IS keyword in queries that test for NULL. This change will not break on systems with Jet 3.5 as Jet 3.5 accepted both types of queries.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce

All three sections use the Northwind sample database that can be installed with Access 97. If installed, it will be located in the Office\Samples subdirectory of your Microsoft Office installation.

MFC ODBC

  1. Create an ODBC DSN in the ODBC Data Sources applet in the Control Panel that points to the Northwind.mdb file.
  2. Create an MFC AppWizard application with database support. Use ODBC and specify the Customers table from the ODBC source created in the previous step as the source for your recordset.
  3. Set a filter on the recordset before it is open. In your view class, insert the following line as the second line in the OnInitialUpdate() method.
    m_pSet->m_strFilter = "Region = NULL";
    						
  4. Create an edit box and bind it to the CustomerID field in the recordset to see the field on the form.
  5. Compile and run the program. No records will be returned.

MFC DAO

  1. Create an MFC AppWizard application with database support. Use DAO and specify the Customers table from the Northwind sample database as the source for your recordset.
  2. Apply the fix from the following article if using a non-unicode build.
    235507  (http://kbalertz.com/Feedback.aspx?kbNumber=235507/EN-US/ ) BUG: DAO 3.6 Causes Debug Errors in MFC DAO Non-Unicode Builds
  3. Insert the following line in the application's InitInstance method to load DAO 3.6 as described in the following article.
    237992  (http://kbalertz.com/Feedback.aspx?kbNumber=237992/EN-US/ ) PRB: Unrecognized Database Format Error with Access 2000
    AfxGetModuleState()->m_dwVersion = 0x0601;
    						
    This will ensure that the Jet 4.0 engine is loaded.
  4. Set a filter on the recordset before it is open. In your view class, insert the following line as the second line in the OnInitialUpdate() method.
    m_pSet->m_strFilter = "Region = NULL";
    						
  5. Create an edit box and bind it to the CustomerID field in the recordset to see the field on the form.
  6. Compile and run the program. No records will be returned.

ADO

  1. Create a Win32 console application.
  2. Insert the following line to use ADO.
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename( "EOF", "adoEOF" )
    						
  3. Paste the following code into your main function:
    CoInitialize(NULL);
    
    try
    {
    	_RecordsetPtr pRs;
    	pRs.CreateInstance(__uuidof(Recordset));
    	pRs->Open(L"SELECT * FROM Customers WHERE Region = NULL",
    				L"Provider=Microsoft.Jet.OLEDB.4.0;"
    				L"Data Source=C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb;"
    				L"Persist Security Info=False",
    				adOpenStatic,
    				adLockOptimistic,
    				-1)<BR/>
    
            if (pRs->adoEOF)
    	        cout << "No records returned.\n";
    
    	while (!pRs->adoEOF)
    	{
    		cout << (char*) (_bstr_t) pRs->GetFields()->GetItem(L"CustomerID")->GetValue() << endl;
    		pRs->MoveNext();
    	}
    
    
    }
    catch (_com_error& e)
    {
    	cout << (char*) e.Description() << endl;
    }
    
    
    						
  4. Change the location of the Northwind.mdb file if necessary to reflect the location of the file on your system.
  5. Compile and run the code. No records will be returned.

Resolution

Change the SQL string in all three sections from = NULL to IS NULL and the recordsets will return the records correctly.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
236991  (http://kbalertz.com/Feedback.aspx?kbNumber=236991/EN-US/ ) PRB: Unrecognized Database Format Error with Access 2000 Database
235507  (http://kbalertz.com/Feedback.aspx?kbNumber=235507/EN-US/ ) BUG: DAO 3.6 Causes Debug Errors in MFC DAO Non-Unicode Builds
152021  (http://kbalertz.com/Feedback.aspx?kbNumber=152021/EN-US/ ) PRB: Deleting Records Containing NULLs Using DAO

APPLIES TO
  • Microsoft Data Access Components 2.1 Service Pack 2
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
  • Microsoft Open Database Connectivity Driver for Access 4.0
  • Microsoft OLE DB Provider for Jet 4.0
Keywords: 
kbdatabase kbjet kbprb kbqfe KB237992
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate