今天搞VB通过ADO连接Excel发现了一个奇怪的问题:
打开VB第一次运行时出现实时错误 '-2147467259(80004005)'
系统不支持所选择的排序按调试可以继续运行且正常,结果后第二次或第三次运行都正常,但如果管理VB重新打开后又在第一次运行时错误我在baidu和google上也搜了一下,也有较多类似的问题,但是没有找到合理有效的解释。Dim adoCnn As new ADODB.Connection
Dim r As new ADODB.Recordset
adoCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\1.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
r.Open "Select * From [" & sheet1$ & "]" , adoCnn, adOpenKeyset, adLockOptimistic在r.open这句出现了错误。请高手指点啊

解决方案 »

  1.   

    这是一个已知的bug,KB中早有解决:http://support.microsoft.com/kb/246167/EN-US/Opening an Microsoft Excel spreadsheet from within Visual Basic in Visual Studio 6.0 Service Pack 3 (or later) Integrated Development Environment (IDE) with the Excel/ODBC/ISAM driver generates the following run-time error: 
    Run time error '-2147467259 (80004005)' : [Microsoft][ODBC Excel Driver]Selected collating sequence not supported by the operating system 
    This problem occurs every time that you run the code within the Visual Basic IDE.NOTE This error does not occur if the all the fields are selected in the SQL statement, for example: SELECT * FROM ...
    Also, this problem does not occur within a compiled EXE. CAUSE
    This is a problem within the Visual Basic IDE starting with service pack 3. 确定一下你的VB是否已经到了sp6 ado是否引用2.x的版本,最好是2.5 excel是用什么版本.
      

  2.   

    果然高手啊,我的VB是SP3,已经有SP6了啊?
    我的MDAC是2.7版本Also, this problem does not occur within a compiled EXE
    以上是否表示编译完的EXE文件就不会有问题?如果这样的话我就可以不管它了。我编译完的EXE运行挺正常的。
      

  3.   


    早SP6了.建议马上打上.
    在VB6中ado引用2.5是最适合.不是说编译的exe就不会有问题,建议谨慎.解决了就结帖吧.
      

  4.   

    如果不想打布丁也可以这样测试一下:on error goto Proc_Exit..... ''这里你的程序Proc_Exit:
    if err.number = -2147467259 then
        resume
    end if这样你就可以跳过第一次错误, 而后边第二第三次错误你说不会发生, 就可以蒙混过去了, HOHO!