PRB: Excel Values Returned as NULL Using DAO OpenRecordset (194124)
The information in this article applies to:
- Microsoft Visual Basic Professional Edition for Windows 4.0
- Microsoft Visual Basic Professional Edition for Windows 5.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 4.0
- Microsoft Visual Basic Enterprise Edition for Windows 5.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
This article was previously published under Q194124 SYMPTOMS
When connecting to an Excel Spreadsheet using the DAO OpenRecordset method,
some values in an Excel column may be returned as Null when the underlying
value is not a Null. This typically occurs when numeric and text datatypes
are intermixed within the same Excel column.
CAUSE
This problem is caused by a limitation of the Excel ISAM driver in that
once it determines the datatype of an Excel column, it will return a Null
for any value that is not of the datatype the ISAM driver has defaulted to
for that Excel column. The Excel ISAM driver determines the datatype of an
Excel column by examining the actual values in the first few rows and then
chooses a datatype that represents the majority of the values in its
sampling.
RESOLUTION
There are two workarounds for this behavior:
- Insure that the data in Excel is entered as text. Just reformatting the
Excel column to Text will not accomplish this. You must re-enter the
existing values after reformatting the Excel column. In Excel, you can
use F5 to re-enter existing values in the selected cell.
- You can add the option IMEX=1; to the Excel connect string in the
OpenDatabase method. For example:
Set Db = OpenDatabase("C:\Temp\Book1.xls", _
False, True, "Excel 8.0; HDR=NO; IMEX=1;")
NOTE: Setting IMEX=1 tells the driver to use Import mode. In this state,
the registry setting ImportMixedTypes=Text will be noticed. This forces
mixed data to be converted to text. For this to work reliably, you may
also have to modify the registry setting, TypeGuessRows=8. The ISAM
driver by default looks at the first eight rows and from that sampling
determines the datatype. If this eight row sampling is all numeric, then
setting IMEX=1 will not convert the default datatype to Text; it will
remain numeric.
You must be careful that IMEX=1 not be used indiscriminately. This is
IMPORT mode, so the results may be unpredictable if you try to do
appends or updates of data in this mode.
The possible settings of IMEX are:
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)
The registry key where the settings described above are located is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\3.5\Engines\Excel
See the REFERENCES section of this article for information on when the
Excel spreadsheet has text column headers with numeric data.
STATUS
This behavior is by design.
REFERENCES
For additional information, please see the following article in the
Microsoft Knowledge Base:
190195
: HOWTO: Extract Information From Excel Sheet with DAO
Modification Type: | Major | Last Reviewed: | 6/24/2004 |
---|
Keywords: | kbprb KB194124 |
---|
|