【问题标题】:CurrentDb.RecordsAffected returns 0. Why?CurrentDb.RecordsAffected 返回 0。为什么?
【发布时间】:2011-03-01 13:19:48
【问题描述】:

如果我将 RecordsAffected 与 CurrentDb.Execute 一起使用,它总是返回 0。如果我首先创建一个数据库对象的实例,它就可以正常工作。为什么?

像这样:

Dim Db As Database
Set Db = CurrentDb

Db.Execute "DELETE * FROM [Samples] WHERE Sample=5"
If Db.RecordsAffected = 0 Then
  MsgBox "Error"
End If

代替:

CurrentDb.Execute "DELETE * FROM [Samples] WHERE Sample=5"
If CurrentDb.RecordsAffected = 0 Then
  MsgBox "Error"
End If

我正在使用 Access 2007 和 Microsoft Office 12.0 Access 数据库引擎对象库。

【问题讨论】:

    标签: ms-access ms-access-2007 dao


    【解决方案1】:

    每次使用 CurrentDB,它都是一个新实例。

    【讨论】:

      【解决方案2】:

      使用With。将您的代码更改为:

      Dim Db As Database
      Dim recordAffect = Integer
      Set Db = CurrentDb
      With Db
        .Execute "DELETE * FROM [Samples] WHERE Sample=5"
        recordAffect = .RecordsAffected
        'If Db.RecordsAffected = 0 Then
        If (recordAffect = 0)  Then
           MsgBox "Error"
        End If
      End With
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-21
        • 2022-01-23
        • 1970-01-01
        • 2011-07-01
        • 2011-01-17
        • 2018-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多