当前位置:网站首页>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"
边栏推荐
- Multiple instances of mysql
- [Prometheus] An optimization record of the Prometheus federation [continued]
- Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
- 经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法
- 防抖和节流有什么区别,分别用于什么场景?
- OneFlow source code analysis: Op, Kernel and interpreter
- ESP8266-Arduino programming example-BMP180 air pressure temperature sensor driver
- [Use of Qt Designer tool]
- 《自然语言处理实战入门》---- 文本样本扩展小技巧:使用回译技术进行样本增强
- ctf.show_web5
猜你喜欢
延时队列优化 (2)
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
kotlin by lazy
Codeblocks + Widgets create window code analysis
while,do while,for循环语句
Pytorch foundation -- tensorboard use (1)
【网站放大镜效果】两种方式实现
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
Codeblocks + Widgets 创建窗口代码分析
随机推荐
基于inquirer封装一个控制台文件选择器
常见链表题及其 Go 实现
电脑死机的时候,发生了什么?
Anaconda Navigator stuck on loading applications
432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
What is the value of biomedical papers? How to translate the papers into Chinese and English?
Redis for infrastructure
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配
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!
尊重客观事实
Swiper轮播图片并播放背景音乐
Mongo for infrastructure
Pytorch foundation -- tensorboard use (1)
Fixed asset visualization intelligent management system
【剑指 Offe】剑指 Offer 18. 删除链表的节点
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
Recommended Books | Recommend 3 database books with rave reviews
怎么样的框架对于开发者是友好的?
[Summary] 1396- 60+ VSCode plugins to create a useful editor