当前位置:网站首页>VBA connects Access database and Excel
VBA connects Access database and Excel
2022-07-30 19:01:00 【OOQ】
Steps to connect VBA to database:
1. Create a connection object
2. Open the connection
3. Execute SQL
4. Close the connection
5. Release the connection object
Premise: in【Tools】>【Reference】ADO Library
Sub conDatabase()Dim con As ADODB.Connection 'declare object variablesSet con = New ADODB.Connection 'Create object variableThe line 'Dim con As New ADODB.Connection ' is equivalent to the above two lines'Establish a database connection'con.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\test.accdb" 'connect to the access database'con.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source=" & _ThisWorkbook.Path & "\data.xlsx"'The following is equivalent to con.OpenWith con.Provider = "microsoft.ace.oledb.12.0".ConnectionString = ThisWorkbook.Path & "\test.accdb".OpenEnd WithMsgBox "Connection succeeded"Dim sql As String'sql = "insert into check(code,year,month,day) values ('1','2022','1','1')"sql = "insert into m_check(m_code,m_year,m_month,m_day) values (3,2022,1,1)"con.Execute (sql)con.Close 'Close the connectionSet con = Nothing 'Release the variableEnd SubNotes:
1. Two ways to create a connection
Dim con As ADODB.Connection 'declare object variablesSet con = New ADODB.Connection 'Create object variableDim con As New ADODB.Connection ’ This line is equivalent to the above two lines2. Two ways to open the connection
con.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\test.accdb" 'Connect access databaseWith con.Provider = "microsoft.ace.oledb.12.0".ConnectionString = ThisWorkbook.Path & "\test.accdb".OpenEnd With
VBA uses SQL to operate Excel, the method is similar to the above connection to Access operation, just change the connection string to the following:
con.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source=" & _ThisWorkbook.Path & "\data.xlsx"
边栏推荐
猜你喜欢
随机推荐
【剑指 Offe】剑指 Offer 18. 删除链表的节点
Anaconda Navigator卡在loading applications
Read the "Language Model" in one article
启动前台Activity
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
你好,我的新名字叫“铜锁/Tongsuo”
7.29模拟赛总结
微信小程序云开发 | 城市信息管理
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
高精度加法
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
Hello, my new name is "Bronze Lock/Tongsuo"
【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
7.30模拟赛总结
阿里云武林头条活动分享
requet.getHeader("token") is null










