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 database table
Keywordsdatabase, table, count records
CategoriesDatabase
 
Use a SQL statement like this one:

    SELECT COUNT (*) FROM table
 
' Return the number of records in the table.
Private Sub cmdCount_Click()
Dim db As Database
Dim rs As Recordset

    On Error GoTo DataError
    
    ' Open the database.
    Set db = OpenDatabase(txtDatabase.Text)

    ' Open the recordset.
    Set rs = db.openrecordset( _
        "SELECT COUNT (*) FROM " & txtTable.Text)
        
    ' Display the result.
    lblCount.Caption = "Table has " & _
        Format$(rs.fields(0)) & " records."

    rs.Close
    db.Close
    
    Exit Sub

DataError:
    MsgBox "Error " & Err.Number & _
        " counting records." & _
        vbCrLf & vbCrLf & Err.Description
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated