Count Property Example

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.

Sub CountX()

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

	' Open recordset with data from Employee table.
	strCnn = "driver={SQL Server};server=srv;" & _
		"uid=sa;pwd=;database=pubs"
	Set rstEmployees = New ADODB.Recordset
	'rstEmployees.CursorType = adOpenStatic
	'rstEmployees.LockType = adLockPessimistic
	rstEmployees.Open "employee", strCnn

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

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

		.Close
	End With

End Sub

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.