Counting the Number of Records in
a Recordset

Counting the Number of Records in
a Recordset

Editor’s Note: This tip is an update to a previous Tip of the Day. Jai Bardhan claims to have a “better solution” for counting records in a recordset.

It is true that the Recordset object, created by the Open or Execute functions of the ADODB object, has a RecordCount property that reports ‘-1,’ if the Recordset has been opened with a forward-only cursor (default). However, you can easily get the proper count using either of the two methods written below. You don’t have to loop through all records and increment a variable, which can be a performance bottleneck. If your recordset has a large number of rows returned, then there’s no reason to iterate through all of them.

The two methods are named Method1 and Method2. With Method1, you can use adUseClient to get the recordcount (utilizing disconnected recordset):

 Private sub Method1          Dim objRecordset As New ADODB.Recordset         Dim RecordCount As Long         Dim objMyConn As New ADODB.Connection         RecordCount = 0             'IMPL Replace with your specific string like "Provider=SQLOLEDB;Data           ' Source=SQLserver;Initial Catalog=yourdb;User Id=sa; Password=;"         objMyConn.ConnectionString = "Your connection string"             ' Added line for disconnected recordset         objMyConn.CursorLocation = adUseClient             objMyConn.Open 

Set objRecordset = objMyConn.Execute("select * from memberpolicy") 'IMPL Not required 'While Not objRecordset.EOF 'RecordCount = RecordCount + 1 'Debug.Print RecordCount 'objRecordset.MoveNext 'Wend Debug.Print objRecordset.RecordCount

end sub

Method2 uses the Movelast method on the recordset instead of looping through:

 Private sub  Method2          Dim objRecordset As New ADODB.Recordset         Dim RecordCount As Long         Dim objMyConn As New ADODB.Connection         RecordCount = 0             'IMPL Replace with your specific string like "Provider=SQLOLEDB;Data           ' Source=SQLserver;Initial Catalog=yourdb;User Id=sa; Password=;"         objMyConn.ConnectionString = "Your connection string"                objMyConn.Open         Set objRecordset = objMyConn.Execute("select * from memberpolicy")         ' IMPL Not required         'While Not objRecordset.EOF         'RecordCount = RecordCount + 1         'Debug.Print RecordCount         'objRecordset.MoveNext         'Wend         ' Add this call instead of looping through the recordset         objRecordset.MoveLast         Debug.Print objRecordset.RecordCount 

end sub

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as