当前位置:网站首页>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"
边栏推荐
猜你喜欢

【PHPWord】Quick Start of PHPWord in PHPOffice Suite

nlohmann json 使用指南【visual studio 2022】

中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳

The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova

Recommendation | People who are kind to you, don't repay them by inviting them to eat

Hello, my new name is "Bronze Lock/Tongsuo"

牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)

Read the "Language Model" in one article

浅聊对比学习(Contrastive Learning)第一弹

固定资产可视化智能管理系统
随机推荐
Chapter 4 Controlling the Execution Flow
阿里云武林头条活动分享
scrapy基本使用
Does the satellite phone communicate directly with the satellite or through a ground station?
Common linked list problems and their Go implementation
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
积性函数
Anaconda Navigator卡在loading applications
OneFlow源码解析:Op、Kernel与解释器
Pytorch基础--tensorboard使用(一)
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
"Ruffian Heng Embedded Bimonthly" Issue 59
MySQL data types
运营 23 年,昔日“国内第一大电商网站”黄了...
Mysql执行原理剖析
启动前台Activity
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
VBA 连接Access数据库和Excle
Codeblocks + Widgets create window code analysis
