Count Property Example (VB)

Microsoft ActiveX Data Objects (ADO)

Count Property Example (VB)

This example demonstrates the Count property with two collections in the Employee database. The property obtains the number of objects in each collection, and sets the upper limit for loops that enumerate these collections. Another way to enumerate these collections without using the Count property would be to use For Each...Next statements.

Public Sub CountX()

    Dim rstEmployees As ADODB.Recordset
    Dim strCnn As String
    Dim intloop As Integer

    ' Open recordset with data from Employee table.
    strCnn = "Provider=sqloledb;" & _
        "Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; "
    Set rstEmployees = New ADODB.Recordset
    rstEmployees.Open "employee", strCnn, , , adCmdTable

    ' Print information about Fields collection.
    Debug.Print rstEmployees.Fields.Count & _
        " Fields in Employee"
    For intloop = 0 To rstEmployees.Fields.Count - 1
        Debug.Print "    " & rstEmployees.Fields(intloop).Name
    Next intloop

    ' Print information about Properties collection.
    Debug.Print rstEmployees.Properties.Count & _
        " Properties in Employee"
    For intloop = 0 To rstEmployees.Properties.Count - 1
        Debug.Print "    " & rstEmployees.Properties(intloop).Name
    Next intloop

    rstEmployees.Close

End Sub