当前位置:网站首页>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 Sub
Notes:
1. Two ways to create a connection
Dim con As ADODB.Connection 'declare object variablesSet con = New ADODB.Connection 'Create object variable
Dim con As New ADODB.Connection ’ This line is equivalent to the above two lines
2. Two ways to open the connection
con.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\test.accdb" 'Connect access database
With 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"
边栏推荐
- 自然语言处理nltk
- 防抖和节流有什么区别,分别用于什么场景?
- (2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
- 【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
- Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
- 实体中增加操作方法
- 怎么样的框架对于开发者是友好的?
- 7.29模拟赛总结
- 第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
- SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
猜你喜欢
解决终极bug,项目最终能顺利部署上线。
攻防世界web-Cat
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
CCNA-子网划分(VLSM)
Swiper rotates pictures and plays background music
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
redis
kotlin by lazy
Codeblocks + Widgets create window code analysis
随机推荐
Pytorch foundation -- tensorboard use (1)
redis
延时队列优化 (2)
Multiple instances of mysql
6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
What is the value of biomedical papers? How to translate the papers into Chinese and English?
深化校企合作 搭建技术技能人才成长“立交桥”
自己需要努力
监听开机广播
线性筛求积性函数
经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法
启动前台Activity
natural language processing nltk
Does the satellite phone communicate directly with the satellite or through a ground station?
【Qt Designer工具的使用】
固定资产可视化智能管理系统
CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
MySQL数据类型
[Summary] 1396- 60+ VSCode plugins to create a useful editor