使用 Delete 方法删除记录Deleting Records Using the Delete Method

使用 Delete 方法将当前记录或记录 对象中的一组记录标记为删除。Using the Delete method marks the current record or a group of records in a Recordset object for deletion. 如果 记录集 对象不允许删除记录,则会发生错误。If the Recordset object does not allow record deletion, an error occurs. 如果处于立即更新模式,则会立即删除数据库中的删除操作。If you are in immediate update mode, deletions occur in the database immediately. 如果由于数据库完整性冲突而无法成功删除记录 (例如) ,则在调用 Update 后,该记录将继续处于编辑模式 If a record cannot be successfully deleted (due to database integrity violations, for example), the record will remain in edit mode after the call to Update. 这意味着,必须先使用 CancelUpdate 取消更新,然后再离开当前记录 (例如,使用 CloseMoveNextRecordset) 。This means that you must cancel the update using CancelUpdate before moving off the current record (for example, using Close, Move, or NextRecordset).

如果处于批处理更新模式,记录将标记为从缓存中删除,并且在调用 UpdateBatch 方法时会发生实际的删除。If you are in batch update mode, the records are marked for deletion from the cache and the actual deletion happens when you call the UpdateBatch method. (若要查看已删除的记录,请在调用 Delete 后将 Filter 属性设置为 adFilterAffectedRecords 。 ) (To view the deleted records, set the Filter property to adFilterAffectedRecords after Delete is called.)

尝试从已删除的记录中检索字段值将生成错误。Attempting to retrieve field values from the deleted record generates an error. 删除当前记录后,删除的记录将保持当前状态,直到您移到另一记录为止。After deleting the current record, the deleted record remains current until you move to a different record. 离开删除的记录后,它将无法再访问。Once you move away from the deleted record, it is no longer accessible.

如果在事务中嵌套删除,则可以使用 RollbackTrans 方法恢复已删除的记录。If you nest deletions in a transaction, you can recover deleted records by using the RollbackTrans method. 如果处于批处理更新模式,可以使用 CancelBatch 方法取消挂起的删除或挂起的删除组。If you are in batch update mode, you can cancel a pending deletion or group of pending deletions by using the CancelBatch method.

如果由于与基础数据发生冲突而导致删除记录的尝试失败 (例如,其他用户) 已删除记录,则该提供程序会将警告返回到 错误 集合,但不会暂停程序执行。If the attempt to delete records fails because of a conflict with the underlying data (for example, a record has already been deleted by another user), the provider returns warnings to the Errors collection but does not halt program execution. 仅当所有请求的记录上发生冲突时才会发生运行时错误。A run-time error occurs only if there are conflicts on all the requested records.

如果设置了 " 唯一表 动态" 属性,并且 记录集 是对多个表执行联接操作的结果,则 delete 方法将仅删除 " 唯一表 " 属性中命名的表中的行。If the Unique Table dynamic property is set and the Recordset is the result of executing a JOIN operation on multiple tables, the Delete method will delete rows only from the table named in the Unique Table property.

Delete 方法采用一个可选参数,该参数可用于指定哪些记录受 删除 操作的影响。The Delete method takes an optional argument that allows you to specify which records are affected by the Delete operation. 此参数的有效值只有下面的一个: The only valid values for this argument are either of the following ADO AffectEnum enumerated constants:

  • adAffectCurrent 仅影响当前记录。adAffectCurrent Affects only the current record.

  • adAffectGroup 仅影响满足当前 Filter 属性设置的记录。adAffectGroup Affects only records that satisfy the current Filter property setting. 筛选器 属性必须设置为 FilterGroupEnum 值或 书签 数组才能使用此选项。The Filter property must be set to a FilterGroupEnum value or an array of Bookmarks to use this option.

下面的代码演示了在调用 Delete 方法时指定 adAffectGroup 的示例。The following code shows an example of specifying adAffectGroup when calling the Delete method. 此示例向示例 记录集 添加一些记录,并更新数据库。This example adds some records to the sample Recordset and updates the database. 然后,它使用 adFilterAffectedRecords 筛选器枚举常量来筛选 记录集,该常量仅保留在 记录集中 可见的新添加的记录。Then it filters the Recordset using the adFilterAffectedRecords filter enumerated constant, which leaves only the newly added records visible in the Recordset. 最后,它将调用 Delete 方法并指定满足当前 Filter 属性设置的所有记录 (应删除新记录) 。Finally, it calls the Delete method and specifies that all of the records that satisfy the current Filter property setting (the new records) should be deleted.

'BeginDeleteGroup  
    'add some bogus records  
    With objRs  
        For i = 0 To 8  
            .AddNew  
            .Fields("CompanyName") = "Shipper Number " & i + 1  
            .Fields("Phone") = "(425) 555-000" & (i + 1)  
            .Update  
        Next i  
  
        ' update  
        .UpdateBatch  
  
        'filter on newly added records  
        .Filter = adFilterAffectedRecords  
        Debug.Print "Deleting the " & .RecordCount & _  
                    " records you just added."  
  
        'delete the newly added bogus records  
        .Delete adAffectGroup  
        .Filter = adFilterNone  
        Debug.Print .RecordCount & " records remain."  
    End With  
'EndDeleteGroup