XL5: Null Fields Returned with Values Using SQL Functions

ID: Q125184

5.00 WINDOWS kbprg kbcode

The information in this article applies to:

SYMPTOMS

In Microsoft Excel, when you use the SQLRetrieve function or the SQLRequest function to return data from an external database, a field in the database that does not contain data may be returned with a value.

CAUSE

This behavior occurs when you use the SQLRetrieve function or the SQLRequest function available in the ODBC Add-in file, XLODBC.XLA, to return data from an external database. When the database contains a column of number data type values, and one of the fields in this column is empty (null), the value in the previous field in the database is returned for this field when the data is returned to a worksheet in Microsoft Excel.

For example, if you use the SQLRetrieve function to return the following data to a worksheet

   Name    Number
   Blue         3
   Yellow
   Red          4

the following data is returned to the worksheet:

   Name    Number
   Blue         3
   Yellow  Yellow
   Red          4

This problem has been reported using the following data sources:

   Microsoft Access 2.0
   DEC 1.10
   Oracle 6
   Oracle 7
   SAS

Note that this problem does not occur with columns of text data type.

WORKAROUND

To work around this behavior, use the functions in the Microsoft Query add-in, the XLQUERY.XLA file to return data from an external database to a worksheet in Microsoft Excel. The following example returns a query from the table "test" in the DB1.MDB database using a Microsoft Access 2.0 data source. The data is returned to cell A1 on Sheet1 in BOOK1.XLS:

   Sub ReturnData()
      QueryGetData "DSN=Access 2.0;DBQ=DB1.MDB;DefaultDir=C:\ACCESS;", _
         "SELECT test.Name, test.Number FROM test test", _
        True, True, False, Range("[BOOK1.XLS]Sheet1!$A$1")
   End Sub

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel version 5.0c.

REFERENCES

For more information about the SQLRetrieve Function, choose the Search button in the Visual Basic Reference and type:

    SQLRetrieve Function

For more information about the QueryGetData Function, choose the Search button in the Visual Basic Reference and type:

    QueryGetData Function

KBCategory: kbprg kbcode KBSubcategory: xlwin

Additional reference words: 5.00 5.00a oracle

Keywords          : xlwin 
Version           : 5.00
Platform          : WINDOWS

Last Reviewed: May 17, 1999