Count 属性范例

该范例使用雇员数据库中的两个集合说明 Count 属性。该属性获得每个集合中的对象数并对枚举这些集合的循环设置上限。另一种不通过使用 Count 属性来枚举这些集合的方法是使用 For Each...Next 语句。

Public Sub CountX()

   Dim rstEmployees As ADODB.Recordset

   Dim strCnn As String

   Dim intloop As Integer

   ' 使用雇员表中的数据打开记录集。

   strCnn = "Provider=sqloledb;" & _

      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "

   Set rstEmployees = New ADODB.Recordset

   rstEmployees.Open "employee", strCnn, , , adCmdTable

   ' 打印有关字段集合的信息。

   Debug.Print rstEmployees.Fields.Count & _

      " Fields in Employee"

   For intloop = 0 To rstEmployees.Fields.Count - 1

      Debug.Print "   " & rstEmployees.Fields(intloop).Name

   Next intloop

   ' 打印属性集合的信息。

   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