NOTE VBA ACCESS

ADO教程

ADO 较DAO更为先进
w3school 教程

增删改查

向ACCESS表中增加记录
Sub newConn()

  Dim i
  Dim CONN As Object
  Dim RST As Object
  Dim SQL As String

  Set CONN = CreateObject("adodb.connection")
  Set RST = CreateObject("adodb.recordset")

  SQL = "INSERT INTO newEmployee(ID,name,data) VALUES(1,'DAO','999')"
  CONN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Employee.accdb"

  Set RST = CONN.Execute(SQL)
  CONN.Close

End Sub
读取ACCESS表记录
Option Explicit

Sub newConn()

  Dim i
  Dim CONN As Object
  Dim RST As Object
  Dim SQL As String

  Sheets("Sheet1").Cells.Clear

  Set CONN = CreateObject("adodb.connection")
  Set RST = CreateObject("adodb.recordset")

  SQL = "select * from newEmployee"

  CONN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Employee.accdb"

  Set RST = CONN.Execute(SQL)

  For i = 1 To RST.Fields.Count
      Cells(1, i) = RST.Fields(i - 1).Name
  Next

  Sheets("Sheet1").[a2].CopyFromRecordset RST

  CONN.Close

End Sub

adodb.connection的provider

通过vb命令可以查询provider 3wschool教程

Debug.Print CONN.Provider ' 结果为 MSDASQL即Microsoft OLE DB provider for ODBC

官方查找相关信息
OLE DB Providers overview

Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

EXCEL的CopyFromRecordset方法

CopyFromRecordset的方法是EXCEL中Range的特有方法 链接

从指定范围的左上角开始,将 ADO 或 DAO Recordset 对象的内容复制到工作表中。如果 Recordset 对象包含具有 OLE 对象的字段,则该方法无效。

语法
. CopyFromRecordset( Data, MaxRows, MaxColumns )
MaxRows,MaxColumns 为可选参数,分别表示拷贝记录上限及字段上限

你可能感兴趣的:(NOTE VBA ACCESS)