在学习VBA的ADO功能时,经常需要把Excel中的数据导入Access中;实现这样的方法有很多种:
1),使用Access的导入功能直接实现;
2),使用ADO的Recordset对象实现,不过由于Access中没有Excel中的表,需要创建表,需要设计表的结构,很繁琐。
3),使用Select查询生成表功能后,代码如下:
Sub ADO连接Excel和Access生成表查询()
Dim con As New ADODB.Connection
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open ThisWorkbook.Path & "\学生管理.accdb"
End With
Dim sql As String
sql = "select * into 数据 from [Excel 12.0;Database=" & ThisWorkbook.Path & "\数据.xlsx].[数据$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
