简体   繁体   English

MS Access 2002 / MS SQL 2008 R2-基于某些条件删除记录的问题

[英]MS Access 2002 / MS SQL 2008 R2 - Problems deleting records based on certain criteria

I'm trying to get my code to delete records from an access 2002 database table that is linked to an MS Sql 2008 database table through a form event. 我正在尝试让我的代码从通过表单事件链接到MS Sql 2008数据库表的Access 2002数据库表中删除记录。 I just want it to delete all records where User is null and Date is null. 我只希望它删除User为null且Date为null的所有记录。

Here's the snippet that's giving me trouble: 这是给我带来麻烦的代码段:

Dim delSQL As String

DoCmd.SetWarnings False
delSQL = "DELETE * FROM Notifications WHERE User IsNull;"
Set rst = CurrentDb.OpenRecordset(delSQL, dbOpenSnapshot)

DoCmd.RunSQL delSQL
rst.Close
Set rst = Nothing
DoCmd.SetWarnings True

I honestly don't know why this isn't working. 老实说,我不知道为什么这行不通。 I ran the SQL statement above (not including the Date part as I wanted to get the User criteria working first) on the SQL server side and it works. 我在SQL Server端运行了上面的SQL语句(不包括Date部分,因为我想让User条件首先生效)。 But every time I try to get it to run in VBA for Access 2002, I get an error when it goes to execute the SQL. 但是,每当我尝试使其在Access 2002的VBA中运行时,执行SQL时都会出错。

User is a reserved word. User是保留字。 Either enclose it in square brackets, or qualify the field name with the table name or alias. 要么将其括在方括号中,要么用表名或别名来限定字段名。

IsNull is a function. IsNull是一个函数。 Either use Is Null as @AlexK. 可以将Is Null用作@AlexK。 suggested or use the field name as the argument to IsNull() . 建议或使用字段名称作为IsNull()的参数。

The * in DELETE * is not required. 不需要*中的DELETE * Access will not throw an error if you include it. 如果包含Access,则不会引发错误。 And it can be useful when you wish to preview the affected rows in the Access query designer's Datasheet View. 当您希望在Access查询设计器的数据表视图中预览受影响的行时,它很有用。

delSQL = "DELETE * FROM Notifications AS n WHERE n.User Is Null"

I suggest you leave SetWarnings on, and use CurrentDb.Execute with dbFailOnError to execute the DELETE . 我建议您将SetWarnings保留为SetWarnings状态,并使用CurrentDb.ExecutedbFailOnError来执行DELETE Since the data source is a linked SQL Server table, also include dbSeeChanges : 由于数据源是链接的SQL Server表,因此还应包含dbSeeChanges

DoCmd.SetWarnings True
CurrentDb.Execute delSQL, dbSeeChanges + dbFailOnError

That approach will give you better information about any problems encountered. 该方法将为您提供有关遇到的任何问题的更好的信息。

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

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