Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleCount the records in a Recordset two ways and compare their speeds
DescriptionThis example shows how to count the records in a Recordset two ways and compare their speeds in Visual Basic 6. It compares the speeds of the queries "SELECT * FROM Authors" and "SELECT COUNT (*) FROM Authors".
Keywordsrecords, count records, ADO
CategoriesDatabase
 
This example executes two queries:

    SELECT * FROM Authors
    SELECT COUNT (*) FROM Authors

In the first case, it then checks the Recordset's RecordCount property to see how many records were selected.

In the second case, the program checks the returned value to see how many records are in the table. In my tests, this method took only 1/5 th as long.

 
Private Sub cmdSelect_Click()
Const DB_FILE = "C:\Program Files\Microsoft Visual " & _
    "Studio\VB98\Biblio.mdb"
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim start_time As Single
Dim i As Long

    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & DB_FILE & ";" & _
        "Persist Security Info=False"
    conn.Open
    start_time = Timer
    For i = 1 To 10
        Set rs = New ADODB.Recordset
        rs.Open "SELECT * FROM AUTHORS", conn, _
            adOpenStatic, , adCmdText
        lblSelectCount.Caption = Format$(rs.RecordCount)
        rs.Close
    Next i

    lblSelectTime.Caption = Format$(Timer - start_time, _
        "0.00")
End Sub

Private Sub cmdSelectCount_Click()
Const DB_FILE = "C:\Program Files\Microsoft Visual " & _
    "Studio\VB98\Biblio.mdb"
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim start_time As Single
Dim i As Long

    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & DB_FILE & ";" & _
        "Persist Security Info=False"
    conn.Open
    start_time = Timer
    For i = 1 To 10
        Set rs = conn.Execute("SELECT COUNT (*) FROM " & _
            "Authors", , adCmdText)
        lblCountCount.Caption = rs.Fields(0)
        rs.Close
    Next i

    lblCountTime.Caption = Format$(Timer - start_time, _
        "0.00")
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated