Retrieve number of fields and rows from Access Database

Posted on 2008-09-30
Last Modified: 2012-05-05

How can I retrieve how many rows and fields are present in a ms access table? (in vb6)

Question by:Schuttend
  • 2
  • 2
  • 2
LVL 76

Expert Comment

ID: 22612476
This is one way. It uses the Microsoft Active Data Objects and the Microsoft ADO Ext for DDL and Security libraries, so you need to set references to them.
Private Sub Command1_Click()
    Dim strConnection As String
    Dim cn As New ADODB.Connection
    Dim cat As New ADOX.Catalog
    Dim rs As New ADODB.Recordset
    Dim r As Long
    Dim strDatabasePath As String
    strDatabasePath = "C:\MyFolder\MyDatabase.mdb"
    Const TableName = "MyTable"
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabasePath & ";Persist Security Info=False"
    cat.ActiveConnection = strConnection
    MsgBox "Field Count = " & cat.Tables(TableName).Columns.Count
    cn.Open strConnection
    rs.Open TableName, cn, adOpenDynamic, adLockPessimistic, adCmdTable
    Do Until rs.EOF
        r = r + 1
    MsgBox "Record count =" & r
End Sub

Open in new window

LVL 16

Accepted Solution

JohnBPrice earned 50 total points
ID: 22613829
You don't actually need ADOX to get just row and field count, do "Select count(*) from table", blah blah blah
to get the row count as rs.fields(0).value,
do "select * from table where 1=2", blah blah blah
and then
the count is  

Author Comment

ID: 22641791
It needs to be a DAO connection.
And I hoped for something without looping through records. Is there no direct call function?

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 76

Expert Comment

ID: 22648428
You can change JohnBPrice's code to DAO by using

set rs = db.OpenRecordSet(strSQL)

instead of rs.Open
LVL 16

Expert Comment

ID: 22672930
GrahamSkan has the DAO version, and it doesn't loop through the records as long as you have a key or index on any column.

Author Closing Comment

ID: 31501871

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 52
Run code from text file in vb 1 73
Modifying Conditional Format from VBA code 3 60
How to read File Date Created using VB6 8 52
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question