ADO 2.7 Samples

Attributes and Name Properties Example (VB)

This example displays the value of the Attributes property for Connection, Field, and Property objects. It uses the Name property to display the name of each Field and Property object.

'BeginAttributesVB

    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string
    
Public Sub AttributesX()

    'recordset and connection variables
   Dim Cnxn As ADODB.Connection
   Dim strCnxn As String
   Dim rstEmployee As ADODB.Recordset
   Dim strSQLEmployee As String
    'record variables
   Dim adoField As ADODB.Field
   Dim adoProp As ADODB.Property
   
   ' Open connection
   strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; "
   Set Cnxn = New ADODB.Connection
   Cnxn.Open strCnxn
   
   ' Open recordset
   Set rstEmployee = New ADODB.Recordset
   strSQLEmployee = "employee"
   'rstEmployee.Open strSQLEmployee, Cnxn, , , adCmdTable
   rstEmployee.Open strSQLEmployee, Cnxn, adOpenForwardOnly, adLockReadOnly, adCmdTable
   'the above two lines openign the recordset are identical as
   'the default values for CursorType and LockType arguments match those shown
  
   ' Display the attributes of the connection
   Debug.Print "Connection attributes = " & Cnxn.Attributes

   ' Display the property attributes of the Employee Table
   Debug.Print "Property attributes:"
   For Each adoProp In rstEmployee.Properties
      Debug.Print "   " & adoProp.Name & " = " & adoProp.Attributes
   Next adoProp
   
   ' Display the field attributes of the Employee Table
   Debug.Print "Field attributes:"
   For Each adoField In rstEmployee.Fields
      Debug.Print "   " & adoField.Name & " = " & adoField.Attributes
   Next adoField

   ' Display fields of the Employee Table which are NULLABLE
   Debug.Print "NULLABLE Fields:"
   For Each adoField In rstEmployee.Fields
      If CBool(adoField.Attributes And adFldIsNullable) Then
         Debug.Print "   " & adoField.Name
      End If
   Next adoField

    ' clean up
   rstEmployee.Close
   Cnxn.Close
   Set rstEmployee = Nothing
   Set Cnxn = Nothing
   
End Sub
'EndAttributesVB

See Also

Attributes Property | Connection Object | Field Object | Name Property | Property Object

© 1998-2001 Microsoft Corporation. All rights reserved.