excel - 在 MS Access 中使用 Excel VBA 进行 SQL 删除

标签 excel ms-access-2010 vba

我正在 Excel 中使用 VBA 从 MS Access 数据库中删除行。 我遇到了

"Error 3704 - Operation is not allowed when the object is open"

虽然当使用类似的代码时,我能够向数据库添加信息。 当尝试删除时,它给了我错误。请帮忙!

Sub DeleteOldValues()

'--------------
'DIM STATEMENTS

Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String, StrQuery As String

'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection

'--------------
'THE CONNECTION OBJECT

strDBName = "Test.accdb"
strMyPath = "Y:"
strDB = strMyPath & "\" & strDBName

 'Connect to a data source:
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB

StrQuery = "DELETE * FROM Table WHERE ProjectName Like '*Project 1*'"

'Performs the actual query
adoRecSet.Open StrQuery, connDB

'--------------
'close the objects
adoRecSet.Close
connDB.Close

'destroy the variables
Set adoRecSet = Nothing  <-error occurs at this point
Set connDB = Nothing

End Sub

最佳答案

您正在删除,因此您还没有记录集。

 'Connect to a data source:
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
StrQuery = "DELETE * FROM Table WHERE ProjectName Like '*Project 1*'"

'Performs the actual query
connDB.Execute strQuery

在大多数情况下,最好使用 DAO with MS Access

More notes on Execute.

关于excel - 在 MS Access 中使用 Excel VBA 进行 SQL 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33631728/

相关文章:

vba - If 和 Do Until 循环 EXCEL VBA

sql - 使用 NOT IN 子查询的 SELECT INTO 查询需要很长时间/挂起

excel - VBA - 通过匹配列和行值来设置范围

java - 如何将 UCanAccess 连接到使用数据库密码加密的 Access 数据库?

performance - VBA I/O 性能

excel - 工作表与工作表(1),我不能从 .net 互操作中做到这一点吗?

java - 如何使用 Apache POI 在不同行创建多个自动筛选器

excel - 添加已安装和引用

python - 如何使用多表将 JSON 转换为 excel 文件?