倒入功能已经实现,就是容错不好。
我用 ssql = "insert INTO [odbc;Driver={SQL Server};" & _
            "Server=" & Trim(bdcxshu!servername) & ";Database=" & Trim(bdcxshu!workdb) & ";" & _
            "UID=" & Trim(bdcxshu!username) & ";PWD=" & Trim(bdcxshu!Password1) & ";].qk_xls " & _
            "select yh,km,ndj,cbpl FROM [$sheet1]"  来指定需要倒入的字段。

解决方案 »

  1.   

    基本上两类方法:1 报错:
      事先查询报错,或用 On Error 拦截报错。2 万能导入:
      就你的例子,事先查询 Excel 表,得到字段名,全部显示在 4 个 ComboBox 中,供用户选择。ssql = "insert INTO [odbc;Driver={SQL Server};" & _
                "Server=" & Trim(bdcxshu!servername) & ";Database=" & Trim(bdcxshu!workdb) & ";" & _
                "UID=" & Trim(bdcxshu!username) & ";PWD=" & Trim(bdcxshu!Password1) & ";].qk_xls " & _
                "select " & combo1 & " as yh," & combo2 & " as km," & combo3 & "as ndj," & combo4 & " as cbpl FROM [$sheet1]"
      

  2.   

    to of123 感谢您的回答
    该过程有on error goto this可程序走到"select yh,km,ndj,cbpl FROM [$sheet1]" 找不到对应字段拦截不到错误,不知道为什么?其实excel中有很多字段,但这4个字段是必须的数据库中也就只有这4个字段,所以再程序中指定。该字段名字是再excel中该的
    原名字为汉字也就是倒入前规范化,也就是要求客户这样做我觉的是个缺点,再有您的第2种方法如果某个combo1没被选中select " & combo1 & " as yh会不会报错?
      

  3.   

    判断是否安装ExcelDim obj As object
    set obj = createobject("Excel.Application")
    if obj is nothing then
        msgbox "没有安装"
    end if