Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieve number of fields and rows from Access Database

Posted on 2008-09-30
6
Medium Priority
?
464 Views
Last Modified: 2012-05-05
Hi,

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

Regards,
Dennis
0
Comment
Question by:Schuttend
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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
        rs.MoveNext
    Loop
    MsgBox "Record count =" & r
    rs.Close
    cn.Close
End Sub

Open in new window

0
 
LVL 16

Accepted Solution

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

Author Comment

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

Regards,
Dennis
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 76

Expert Comment

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

set rs = db.OpenRecordSet(strSQL)

instead of rs.Open
0
 
LVL 16

Expert Comment

by:JohnBPrice
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.
0
 

Author Closing Comment

by:Schuttend
ID: 31501871
thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

598 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