如何计算访问中受影响的记录?

时间:2019-05-10 11:49:47

标签: ms-access

我正在尝试获取一个查询影响了多少条记录,以了解它是否成功,如果不成功,我将运行另一个查询。

我从一个想打开查询并检查

的表单中获取参数
UPDATE attendance SET attendance.[attendance days] = [Forms]![attendace updater]![days]+[attendance days], 
attendance.[attendance hours] = [Forms]![attendace updater]]![hours]+[attendance hours]
WHERE (((attendance.[id])=[Forms]![attendace updater]]![id]) AND ((attendance.year)= 
[Forms]![attendace updater]]![year]) AND ((attendance.month)=[Forms]![attendace updater]]! 
[month]));

在出勤表中:月是数字,年是数字,出勤天数是数字,出勤时间是数字,而id是文本...

谢谢:)

2 个答案:

答案 0 :(得分:0)

Private Sub btn_Click()

    'create reference to you database object
    Dim myDb As dao.Database
    Set myDb = CurrentDb

    'create ref to query definition object within scope of database object
    Dim qdf as QueryDef
    Set qdf = myDb.QueryDefs("queryName")

    qdf.Execute
    MsgBox qdf.RecordsAffected

End Sub

如果db.execute需要一个参数,则可能是因为它没有正确地转义引号。

答案 1 :(得分:0)

如果您希望能够在运行查询之前查看将影响多少条记录,而不是仅查看运行后可能会受到的影响,则可以使用类似以下的内容:

SELECT COUNT(*) AS RecordCount FROM [MyTable] WHERE <Same WHERE clause as the UPDATE I am testing>

对于特定查询:

SELECT COUNT(*) AS RecordCount
FROM attendance
WHERE (((attendance.[id])=[Forms]![attendace updater]]![id]) AND ((attendance.year)= [Forms]![attendace updater]]![year]) AND ((attendance.month)=[Forms]![attendace updater]]![month]))

修改

VBA中,您可以使用DCount。以下示例假定您正在VBA形式的上下文中运行[attendace updater]

DCount("*","attendance","id='" & Me.id & "' AND year=" & Me.year & " AND month=" & Me.month)