当前位置:网站首页>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.值为数字时,不应使用单引号,即使表字段类型是文本类型。
参考:
边栏推荐
- 【Qt Designer工具的使用】
- One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...
- 生物医学论文有何价值 论文中译英怎样翻译效果好
- 一文读懂“语言模型”
- kotlin by lazy
- 实体中增加操作方法
- arcpy获取要素类(属性表)包含的数目
- 终端分屏工具Terminalx的使用
- 【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
- 【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
猜你喜欢
随机推荐
vxe-table实现复选框鼠标拖动选中
几个GTest、GMock的例子
设计消息队列存储消息数据的 MySQL 表格
A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....
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!
【剑指 Offe】剑指 Offer 17. 打印从1到最大的n位数
Pytorch foundation -- tensorboard use (1)
Web结题报告
ROS 环境使用第三方动态链接库(.so)文件
Delay queue optimization (2)
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
core sound driver详解
高并发秒杀项目总结
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
MySQL——基础知识
固定资产可视化智能管理系统
arcpy获取要素类(属性表)包含的数目
WEBSOCKETPP使用简介+demo
requet.getHeader(“token“) 为null
LeetCode Exercise - Two Questions About Finding Sum of Array Elements