Access Tip – Generic function to return the number of records
The following are some samples of VBA code that may prove useful.
This is a generic function to check the number of records. In the calling program, create an SQL statement stored in a string. Say you want to know the number of records and store in lngRecs. If the string was called strSQL, you would call this function using
Dim lngRecs as Long
Dim strSQL as String
strSQL = “SELECT * from tblNames”
lngRecs = funRecordCount(strSQL)
Public Function funRecordCount(strSQL As String) As Integer Dim dbs As Database Dim rst As Recordset On Error GoTo Error_funRecordCount Set dbs = CurrentDb ‘ Find the number of records. First test records were found. funRecordCount = 0 ‘ No records found Else rst.MoveLast ‘ End of the recordset End If Exit_funRecordCount: Error_funRecordCount: Resume Exit_funRecordCount End Function |