Count property (ADO)

Home | About me | EXCEL VB Programming (XL97-2003) | ACCESS Programming | EXCEL VB.Net Programming | EXCEL Spreadsheet Functions Material Management  |  Guestbook
 

ADO Count property

The Count property returns a long value that is the number of items in the collection.

The counting starts at zero. You can use this value to loop through the collection by iterating from zero to the value of Count property minus one. If you are using Microsoft Visual Basic and want to loop through the members of a collection without checking the Count property, use the For Each...Next command.

Applies To :
Axes Collection (ADO MD)
Columns Collection (ADOX)
CubeDefs Collection (ADO MD)
Dimensions Collection (ADO MD)
Errors Collection (ADO)
Fields Collection (ADO)
Groups Collection (ADOX)
Hierarchies Collection (ADO MD)
Indexes Collection (ADOX)
Keys Collection (ADOX)
Levels Collection (ADO MD)
Members Collection (ADO MD)
Parameters Collection (ADO)
Positions Collection (ADO MD)
Procedures Collection (ADOX)
Properties Collection (ADO)
Tables Collection (ADOX)
Users Collection (ADOX)
Views Collection (ADOX)

Example:

Code:
intCountNumber = objRecord.Fields.Count

Example:

' Purpose: The Count property obtains the number of objects in each collection, and sets
' the upper limit for loops that enumerate these collections.

Sub CountProperty()
On Error GoTo ErrorHandler

'  Recordset and Connection variables
Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim strConnection As String
Dim intLoop As Integer

' Open a connection
Set cnn = New ADODB.Connection
strConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source=D:\MyData\Northwind.mdb;"
cnn.Open strConnection

' Open recordset with data from 'Employees' table
strSQL = "Employees"
rst.Open strSQL, cnn, adOpenForwardOnly, adLockReadOnly, adCmdTable

' Print information about Fields collection
Debug.Print rst.Fields.Count & " Fields in Employees"

For intLoop = 0 To rst.Fields.Count - 1
Debug.Print " " & rst.Fields(intLoop).Name
Next intLoop

' Print information about Properties collection
Debug.Print rst.Properties.Count & " Properties in Employees"

For intLoop = 0 To rst.Properties.Count - 1
  Debug.Print " " & rst.Properties(intLoop).Name
Next intLoop

' Clean up objects
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub

ErrorHandler:
' Clean up objects
If Not rst Is Nothing Then
   If rst.State = adStateOpen Then rst.Close
End If
Set rst = Nothing

If Not cnn Is Nothing Then
   If cnn.State = adStateOpen Then cnn.Close
End If
Set cnn = Nothing

If Err <> 0 Then
   MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub