ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 向您问好

[原创]一个绝招解决SQL数字文本混合查询(特简单)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-8-28 05:34 | 显示全部楼层
本帖已被收录到知识树中,索引项:SQL应用
这个示例程序,在Excel2003下问题存在,而在Excel 2000可用

TA的精华主题

TA的得分主题

发表于 2012-12-28 09:28 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2016-10-30 20:58 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2016-10-30 21:04 | 显示全部楼层
https://support.microsoft.com/en-us/kb/194124
微软的技术文档

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.
MORE INFORMATION
Steps to Reproduce Behavior

To duplicate this problem, first create an Excel Workbook with a default Sheet1 spreadsheet. In the first column of Sheet1, enter the following values - 123, aaa, 456, bbb, 789. Save this Workbook in your C:\Temp directory, and name it Book1.XLS.

In Visual Basic, create a new Standard EXE project and follow these steps:
Make a reference to Microsoft DAO 3.5 Object Library. In Visual Basic 6.0, this will be Microsoft DAO 3.51 Object Library.
Add a CommandButton to the new Form.
Place the following code in the Form's General Declarations section:

      Dim Db As Database
      Dim Rs As Recordset

      Private Sub Command1_Click()
          Set Rs = Db.OpenRecordset("Sheet1$")
          'This will print the spreadsheet Text values as Nulls.

          Do While Not Rs.EOF
              Debug.Print Rs(0)
              Rs.MoveNext
          Loop

      End Sub

      Private Sub Form_Load()
          'HDR refers to the Excel header row.
      Set Db = OpenDatabase("C:\Temp\Book1.xls", _
               False, True, "Excel 8.0; HDR=NO;")

      End Sub

      Private Sub Form_Unload(Cancel As Integer)
          Db.Close
          Set Db = Nothing

      End Sub

                                               
Run the Project by pressing the F5 key and note that in the Debug window the text values are printed as Null. If the majority of the values in the Excel Spreadsheet were text, then the result from the code above would be reversed. That is, the numeric values would come back as Nulls.
REFERENCES
For additional information, please see the following article in the Microsoft Knowledge Base:

190195 : HOWTO: Extract Information From Excel Sheet with DAO
Properties
Article ID: 194124 - Last Review: 12/05/2015 09:27:48 - Revision: 4.0

Applies to
Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 4.0 Enterprise Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 6.0 Enterprise Edition

TA的精华主题

TA的得分主题

发表于 2017-7-12 17:22 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
做一记号,以后查看。

TA的精华主题

TA的得分主题

发表于 2020-2-27 19:15 | 显示全部楼层
感觉这个sql不靠谱啊,怎么搞错数据。

TA的精华主题

TA的得分主题

发表于 2020-2-27 20:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
yzxfelix 发表于 2020-2-27 19:15
感觉这个sql不靠谱啊,怎么搞错数据。

ADO主要用于数据库,用在EXCEL中可用修改注册表的方法解决数字文本混排
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-12-5 02:51 , Processed in 0.046185 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表