>

Count Property

Applies To

Containers Collection, Databases Collection, Documents Collection, Errors Collection, Fields Collection, Groups Collection, Indexes Collection, Parameters Collection, Properties Collection, QueryDefs Collection, Recordsets Collection, Relations Collection, TableDefs Collection, Users Collection, Workspaces Collection.

Description

Returns the number of objects in a collection.

Return Value

This property's return value is a Long integer data type.

Remarks

Because members of a collection are numbered starting with 0, loops are always coded starting with the 0 member; for example:


    For I = 0 to Databases.Count - 1
The Count property setting is never Null. If its value is 0, there are no objects in the collection.

See Also

Append Method, Delete Method, Refresh Method.

Example

This example lists the names and total number of TableDef and QueryDef objects in the current database.


Dim dbsLocal As Database, intCount As Integer
Set dbsLocal = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")


For intCount = 0 To dbsLocal.TableDefs.Count - 1
Debug.Print dbsLocal.TableDefs(intCount).Name
Next intCount Debug.Print dbsLocal.TableDefs.Count For intCount = 0 To dbsLocal.QueryDefs.Count - 1
Debug.Print dbsLocal.QueryDefs(intCount).Name
Next intCount Debug.Print dbsLocal.QueryDefs.Count
Example (Microsoft Access)

The following example prints the names and total number fields in an Orders table.


Sub CountFields()
    Dim dbs As Database, tdf as TableDef
    Dim intI As Integer

    ' Return Database variable that points to current database.
    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs!Orders
    ' Count fields in Fields collection of TableDef object.
    Debug.Print tdf.Fields.Count
    ' List names of all fields.
    For Each fld In tdf.Fields
        Debug.Print fld.Name
    Next fld
End Sub
Example (Microsoft Excel)

This example displays the number of recordsets in the NWINDEX.MDB database and then enters the names on Sheet1.

To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.


Dim db As Database, i As Integer
Sheets("Sheet1").Activate
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
Cells(1, 1).Value = "TableDef list for " & db.Name
Cells(1, 1).EntireColumn.AutoFit
For i = 0 To db.TableDefs.Count - 1
    Cells(i + 2, 1).Value = db.TableDefs(i).Name
Next i
MsgBox "There are " & db.TableDefs.Count & " TableDefs"
db.Close