您的位置:程序门 -> vb -> vba



求问:在access数据库导入excel过程中有错   请帮忙


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求问:在access数据库导入excel过程中有错 请帮忙
发表于:2007-08-03 13:08:47 楼主
在access数据库导入excel过程中有错    
代码如下
dim   export_str       as   string
dim   conn                   as   new   adodb.connection
conn.open   "provider=microsoft.jet.oledb.4.0;data   source=d:\test.mdb; "
export_str   =   "select   *     into     [database=d:\excel.xls].[sheet1]     from   table "
可以连接到数据库     但是不能导入进去  
请教高手   谢谢先
发表于:2007-08-08 10:35:281楼 得分:0
excel   文件好像也要建数据源的说
发表于:2007-08-08 11:15:512楼 得分:0
'   create   a   recordset   object.
参考以下代码:
dim   rspubs   as   adodb.recordset
set   rspubs   =   new   adodb.recordset

with   rspubs
'   assign   the   connection   object.
.activeconnection   =   cnpubs
'   extract   the   required   records.
.open   "select   *   from   authors "
'   copy   the   records   into   cell   a1   on   sheet1.
sheet1.range( "a1 ").copyfromrecordset   rspubs

'   tidy   up
.close
end   with

cnpubs.close
set   rspubs   =   nothing
set   cnpubs   =   nothing
发表于:2007-08-08 12:02:153楼 得分:0
我这是我程序里的一个函数,con,是access的连接[全局的],引用到excel对象.
tablename   数据库的表名.
ws   EXECel的工作表.
private   function   table2sheet(tablename   as   string,   ws   as   excel.worksheet)   as   boolean
        dim   x   as   long,   y   as   long
        dim   ystr   as   string
        dim   rs   as   recordset
        dim   max   as   long,   sbstep   as   single,   sbvalue   as   long,   sbheight   as   long
 
        set   rs   =   con.EXECute( "select   *   from   [ "   &   tablename   &   "] ")
       
        for   x   =   0   to   rs.fields.count   -   1
                ws.cells(1,   x   +   1).value   =   rs.fields(x).name
        next
        ws.range(ws.cells(1,   1),   _
                ws.cells(1,   rs.fields.count)).font.bold   =   true
               
        y   =   2
        do   until   rs.eof
                ws.range( "a "   &   cstr(y)).copyfromrecordset   rs,   51
                y   =   y   +   51
                doevents
                me.caption   =   "[ "   &   tablename   &   "]current   record: "   &   cstr(y   -   1)
                sbvalue   =   sbvalue   +   sbstep
        loop
   
        table2sheet   =   true
end   function


快速检索

最新资讯
热门点击