简体   繁体   English

使用ADODB命令真正删除MS Access数据库的条目

[英]Really deleting entries of MS Access database with ADODB command

I would like to delete rows of an access table. 我想删除访问表的行。 Therefore I am using VBA with an ADODB command object. 因此,我将VBA与ADODB命令对象一起使用。

The problem is that instead of really deleting the row, the query just sets all values except the ID to 0 / "" 问题是查询没有真正删除行,而是将ID以外的所有值都设置为0 /“”

What am I doing wrong or what do I need to do instead to really delete the entry? 我在做错什么还是要真正删除该条目我需要做什么?

Thank you in advance! 先感谢您!

Code: 码:

Dim db1 As ADODB.Connection
Dim db_data As ADODB.Recordset
Dim cmd As ADODB.Command

...'Code to build up Connection'

Set cmd = New ADODB.Command

cmd.ActiveConnection = db1
cmd.CommandText = "DELETE FROM TableXY WHERE FieldXY = ValueXY"
Set db_data = cmd.Execute

Try to explicitly delete all: 尝试显式删除所有内容:

cmd.CommandText = "DELETE * FROM TableXY WHERE FieldXY = ValueXY"

You can also skip assigning it to a recordset: cmd.Execute instead of Set db_data = cmd.Execute . 您也可以跳过将其分配给记录集的方法: cmd.Execute而不是Set db_data = cmd.Execute

Note that I can't replicate your issue. 请注意,我无法复制您的问题。

ValueXY must have a value: ValueXY必须具有一个值:

ValueXY = SomeValue
cmd.CommandText = "DELETE FROM TableXY WHERE FieldXY = " & ValueXY & ""

or, if text: 或者,如果是文字:

cmd.CommandText = "DELETE FROM TableXY WHERE FieldXY = '" & ValueXY & "'"

You can simply do this without using the command: 您可以简单地执行此操作,而无需使用命令:

Dim db1 As New ADODB.Connection
db1.ConnectionString = "your connection string here"
db1.Open
db1.Execute "DELETE FROM TableXY WHERE FieldXY = ValueXY"
db1.Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM