Attributes and Name Properties Example (VB)

Microsoft ActiveX Data Objects (ADO)

ADO 2.5 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 Main()
    On Error GoTo ErrorHandler

    'recordset and connection variables
    Dim Cnxn As ADODB.Connection
    Dim strCnxn As String
    Dim rstEmployees 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='MySqlServer';" & _
        "Initial Catalog='Pubs';Integrated Security='SSPI';"
    Set Cnxn = New ADODB.Connection
    Cnxn.Open strCnxn
   
    ' Open recordset
    Set rstEmployees = New ADODB.Recordset
    strSQLEmployee = "employee"
    rstEmployees.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 rstEmployees.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 rstEmployees.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 rstEmployees.Fields
        If CBool(adoField.Attributes And adFldIsNullable) Then
            Debug.Print "   " & adoField.Name
        End If
    Next adoField

    ' clean up
    rstEmployees.Close
    Cnxn.Close
    Set rstEmployees = Nothing
    Set Cnxn = Nothing
    Exit Sub
    
ErrorHandler:
    ' clean up
    If Not rstEmployees Is Nothing Then
        If rstEmployees.State = adStateOpen Then rstEmployees.Close
    End If
    Set rstEmployees = Nothing
    
    If Not Cnxn Is Nothing Then
        If Cnxn.State = adStateOpen Then Cnxn.Close
    End If
    Set Cnxn = Nothing
    
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub
'EndAttributesVB

See Also

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

© 1998-2003 Microsoft Corporation. All rights reserved.