当前位置:网站首页>VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
2022-07-30 18:50:00 【OOQ】
问题描述:初次尝试使用VBA去操作Access数据库,执行 INSERT 操作的时候,发生了“运行时错误'-2147217900(80040e14):自动化(Automation)错误”异常。
图 1
Sub 连接()
Dim con As ADODB.Connection '声明对象变量
Set con = New ADODB.Connection '创建对象变量
Dim sql As String
'建立数据库连接
'con.Open "provider=microsoft.ace.oledb.12.0;data source =" & ThisWorkbook.Path & "\test.accdb" '连接access数据库
'以下写法等效于 con.Open
With con
.Provider = "microsoft.ace.oledb.12.0"
.ConnectionString = ThisWorkbook.Path & "\test.accdb"
.Open
End With
MsgBox "连接成功"
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.在Access SQL 查询分析器执行以下SQL,结果类似如图2所示是通过的。但是VBA运行仍然不通过
SQL insert into check(code,year,month,day) values ('1','2022','1','1')2.考虑是不是因为使用了 code 、year 、month 、day 等字段所引起的,所以将这几个字段更改成了 m_code、m_year、m_month、m_day。问题还是存在。
3.检查字段名前后有无空格
4.参考许多案例,发现值是为数字的时候,不需要加 ''(单引号)。【字段类型设置是短文本类型】,问题解决。如图3成功
sql = "insert into m_check(m_code,m_year,m_month,m_day) values (3,2022,1,1)"

图 2

图 3
总结:
1. 数据库表字段名称不能使用关键字
2.表字段不能使用空格和其他特殊字符
3.排除SQL语法错误
4.值为数字时,不应使用单引号,即使表字段类型是文本类型。
参考:
边栏推荐
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 常见链表题及其 Go 实现
- 延时队列优化 (2)
- 卫星电话是直接与卫星通信还是通过地面站?
- 2种手绘风格效果比较,你更喜欢哪一种呢?
- Chapter 4 Controlling the Execution Flow
- 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
- AI基础:图解Transformer
- MySQL data types
- Chapter 14 Type Information
猜你喜欢

一文读懂“语言模型”

Scrapy框架介绍

nlohmann json 使用指南【visual studio 2022】
![[Summary] 1396- 60+ VSCode plugins to create a useful editor](/img/e4/65e55d0e4948c011585b72733d4d19.jpg)
[Summary] 1396- 60+ VSCode plugins to create a useful editor

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

Recommended Books | Recommend 3 database books with rave reviews

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

使用postman调接口报Content type ‘text/plain;charset=UTF-8‘ not supported

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
随机推荐
Spark学习:用spark实现ETL
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
Web结题报告
What kind of framework is friendly to developers?
设计消息队列存储消息数据的 MySQL 表格
MySQL——基础知识
基于inquirer封装一个控制台文件选择器
怎么样的框架对于开发者是友好的?
Multiple instances of mysql
CCNA-子网划分(VLSM)
LeetCode 练习——关于查找数组元素之和的两道题
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
【剑指 Offe】剑指 Offer 18. 删除链表的节点
智慧中控屏
固定资产可视化智能管理系统
终端分屏工具Terminalx的使用
Pytorch基础--tensorboard使用(一)
线性筛求积性函数
【PHPWord】PHPOffice 套件之PHPWord快速入门
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显
