DAO/链接表/ODBC
Access的DAO准确获取记录集Recordset的记录数Recordcount
2017-05-28 06:14:44

DAO准确获取记录集Recordset的记录数Recordcount

在使用Access过程中,我们经常要使用Dao对数据进行相关处理,但我们发现,有时用记录集Recordset获取的记录数Recordcount并不准确,明明数据有很多,但得到的结果只有1,特别是在多用户使用环境,多个用户同时新增 修改 删除记录的时候,这种情况会更容易出现

那在Acccess中,如果更加准确地获取记录集Recordset的记录数Recordcount呢

下面就是一个简单的函数,可以准确获得dao的记录数

Function FindRecordCount(strSQL As String) As Long Dim dbsNorthwind As DAO.Database Dim rstRecords As DAO.Recordset On Error GoTo ErrorHandler  Set dbsNorthwind = CurrentDb  Set rstRecords = dbsNorthwind.OpenRecordset(strSQL)  If rstRecords.EOF Then FindRecordCount = 0  Else rstRecords.MoveLast FindRecordCount = rstRecords.RecordCount  End If  rstRecords.Close  dbsNorthwind.Close  Set rstRecords = Nothing  Set dbsNorthwind = Nothing Exit Function ErrorHandler:  MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description End Function