Properties Collection and Property Object

The Properties collection and Property object provide information about the characteristics of the Connection, Command, Recordset, and Field objects. The Properties collection can be accessed through any of these objects, and the Property object can be accessed through the Properties collection by using the default indexing method.

The Properties collection consists of Property objects. In addition to returning the value and type for a property, the Property object provides attributes of a property. Attributes describe things such as whether the specific property of an object is supported or required, or whether it can be read or written. For example, ConnectionTimeout is a property that provides information about the number of seconds to wait to establish a connection before returning a time-out error.

You may find it useful to enumerate through the Properties collection of an object. Example B shows code you can write to list each property.

Examples
A. Retrieving the ConnectionTimeout, CommandTimeout, and Updatability properties.

Dim cn As New ADODB.Connection

Dim cmd As New ADODB.Command

Dim rs As New ADODB.Recordset

    

cn.Provider = "sqloledb"

cn.Properties("Data Source").Value = "MyServerName"

cn.Properties("Initial Catalog").Value = "pubs"

cn.Properties("Integrated Security").Value = "SSPI"

cn.Open

  

' Find ConnectionTimeout property.

Debug.Print cn.Properties("ConnectionTimeout")

    

Set Cmd.ActiveConnection = Cn

cmd.CommandText = "titles"

cmd.CommandType = adCmdTable

Set rs = cmd.Execute

    

' Find CommandTimeout property.

Debug.Print cmd.Properties("CommandTimeout")

    

Debug.Print rs.Properties("Updatability")

  

The Properties collection is retrieved through the Connection, Command, and Recordset objects. The ConnectionTimeout property of the Connection object is then printed. The same steps are performed for the Command and Recordset objects.

B. Enumerating through the Properties collection for an object.

The following code shows a method for listing each property of an object, using a Connection object and Recordset object as examples.

Dim cn As New ADODB.Connection

Dim rs As ADODB.Recordset

  

cn.Provider = "sqloledb"

cn.Properties("Data Source").Value = "MyServerName"

cn.Properties("Initial Catalog").Value = "pubs"

cn.Properties("Integrated Security").Value = "SSPI"

cn.Open

  

Set rs = New ADODB.Recordset

rs.Open "select * from authors", cn

  

' Create a variable to list the properties.

Dim prop As ADODB.Property

  

' Enumerate through the properties of the Connection object.

For Each prop In cn.Properties

    Debug.Print prop.Name, prop.Value, prop.Attributes

Next

  

' Enumerate through the properties of the Recordset object.

For Each prop In rs.Properties

    Debug.Print prop.Name, prop.Value, prop.Attributes

Next

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.