Office中国论坛/Access中国论坛

标题: Excel不引用DAO直接对Access数据库操作 [打印本页]

作者: 盗梦    时间: 2015-4-4 10:05
标题: Excel不引用DAO直接对Access数据库操作
本帖最后由 盗梦 于 2015-4-4 10:07 编辑

一般Excel要读写Access数据库,需要引用DAO或ADO数据库操作组件以下方法 是不引用DAO对数据库操作 (摘录自网络)  

  1. Sub DaoWithoutReferences()       '定义变量.      
  2.     Dim dbEng As Object      
  3.    Dim Db As Object      
  4.    Dim Rs As Object       '使用OLE方式创建dbEngine对象。Set the dbEng object using OLE      
  5.    Set dbEng = CreateObject("DAO.DBEngine")      
  6.    'NOTE: In Microsoft Excel 97, use this line of code instead of the       'above line of code:       '      
  7.    '   Set dbEng = CreateObject("DAO.DBEngine.35")       '打开数据库 Open a database. Note that the statement contains the dbEng object.      
  8.      Set Db = _           
  9.         dbEng.workspaces(0).opendatabase("c:\MSOffice\Access" & _           
  10.          "Samples\Northwind.mdb")       '在数据库中打开记录信 Open a recordset in the database.      
  11.          Set Rs = Db.openrecordset("Customers")      
  12.          '获取数据库记录数 Perform a move last and find the number of records       'in the database to test if the operation worked.      
  13.           Rs.movelast      
  14.            MsgBox Rs.recordcount      
  15.           Set Rs = Nothing      
  16.          Set Db = Nothing      
  17.          Set dbEng = Nothing   
  18. End Sub               
复制代码



作者: fatman    时间: 2015-4-13 14:21
谢谢您的分享!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3