MS Acess Macro to记录每个查询所影响的行数

时间:2016-02-03 15:02:17

标签: ms-access

我正在运行一系列访问查询以更新几个表,并且在运行每个查询时,我会弹出消息,说明要影响的行数。有没有办法使用宏来记录受影响到表格的行数,甚至记录到excel表格?

以下是根据您的反馈创建的代码。

Option Compare Database
Option Explicit

Public Sub GenerateReports()

Dim strSQL1 As Object
Dim strSQL2 As Object


DoCmd.SetWarnings False

'First run the SELECT query to find out how many rows are effected

Set strSQL1 = "SELECT * FROM Count_Number_of_Rows WHERE Field1 = 'query1'"
Set rec1 = CurrentDb.OpenRecordset(strSQL1)

rec1.MoveFirst
rec1.MoveLast
MyRecNums = rec1.RecordCount

'Some code to write MyRecNums to a table
'Blah blah

'Now run the actual Update query

CurrentDb.Execute "query1", dbFailOnError

Set strSQL2 = "UPDATE Count Number of Rows SET Field2 = 'query2' WHERE Field1 = 'query1'"
DoCmd.ExecuteSQL strSQL2


DoCmd.SetWarnings True


End Sub

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

漫长的过程(也是我能想到的唯一方法)是使用Select查询镜像每个Update查询。

'First run the SELECT query to find out how many rows are effected
Set strSQL1 = "SELECT * FROM MyTable WHERE MyField = 'SomeValue'"
Set rec1 = CurrentDB.OpenRecordset(strSQL1)

rec1.MoveFirst
rec1.MoveLast
MyRecNums = rec1.RecordCount

'Some code to write MyRecNums to a table
'Blah blah

'Now run the actual Update query
Set strSQL2 = "UPDATE MyTable SET MyField2 = 'Whatever' WHERE MyField = 'SomeValue'"
DoCmd.ExecuteSQL strSQL2