当前位置:网站首页>VBA 连接Access数据库和Excle
VBA 连接Access数据库和Excle
2022-07-30 18:50:00 【OOQ】
VBA 连接数据库步骤:
1.创建连接对象
2.打开连接
3.执行SQL
4.关闭连接
5.释放连接对象
前提:在【工具】>【引用】ADO库
Sub conDatabase()
Dim con As ADODB.Connection '声明对象变量
Set con = New ADODB.Connection '创建对象变量
'Dim con As New ADODB.Connection ’这一行等效于以上两行
'建立数据库连接
'con.Open "provider=microsoft.ace.oledb.12.0;data source =" & ThisWorkbook.Path & "\test.accdb" '连接access数据库
'con.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source =" & _
ThisWorkbook.Path & "\数据.xlsx"
'以下写法等效于 con.Open
With con
.Provider = "microsoft.ace.oledb.12.0"
.ConnectionString = ThisWorkbook.Path & "\test.accdb"
.Open
End With
MsgBox "连接成功"
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 '关闭连接
Set con = Nothing '释放变量
End Sub
注意事项:
1. 创建连接的两种写法
Dim con As ADODB.Connection '声明对象变量 Set con = New ADODB.Connection '创建对象变量Dim con As New ADODB.Connection ’这一行等效于以上两行2.两种写法打开连接
con.Open "provider=microsoft.ace.oledb.12.0;data source =" & ThisWorkbook.Path & "\test.accdb" '连接access数据库With con .Provider = "microsoft.ace.oledb.12.0" .ConnectionString = ThisWorkbook.Path & "\test.accdb" .Open End With
VBA使用SQL操作Excel,方法和上面连接Access操作相似只需将连接字符串更改成下面这种:
con.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source =" & _ ThisWorkbook.Path & "\数据.xlsx"
边栏推荐
- What kind of framework is friendly to developers?
- The use of terminal split screen tool Terminalx
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
- Scrapy framework is introduced
- 7.30模拟赛总结
- Presto 中 lookUp Join的实现
- Codeblocks + Widgets create window code analysis
- 荐号 | 对你有恩的人,不要请吃饭来报答
- C# wpf 无边框窗口添加阴影效果
猜你喜欢

【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list

攻防世界web-Cat

scrapy基本使用

cocos creater 热更重启导致崩溃

固定资产可视化智能管理系统

Fixed asset visualization intelligent management system

荐号 | 对你有恩的人,不要请吃饭来报答

中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用

微博广告分布式配置中心的构建与实践(有彩蛋)

《痞子衡嵌入式半月刊》 第 59 期
随机推荐
AWS console
What is the value of biomedical papers? How to translate the papers into Chinese and English?
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
Recommendation | People who are kind to you, don't repay them by inviting them to eat
深化校企合作 搭建技术技能人才成长“立交桥”
MySql中@符号的使用
3D机器视觉厂商的场景争夺战役
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
一文读懂“语言模型”
core sound driver详解
Multiple instances of mysql
432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
Recommended Books | Recommend 3 database books with rave reviews
开心的聚餐
Delay queue optimization (2)
ctf.show_web5
基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
7.30模拟赛总结
自然语言处理nltk
