Inherited Property Example (MDB)

The following example creates a Property object in the Properties collection of a TableDef object, and then creates a new QueryDef object based on the same table. The Property object automatically exists in the Properties collection of the new QueryDef object. Next, the procedure checks the Inherited property for the Property objects in the Properties collections of both the TableDef and the QueryDef objects.

In this example, the Microsoft Access DatasheetFontItalic property is created and appended to the Properties collection of the TableDef object. The DatasheetFontItalic property is defined by Microsoft Access rather than by the Microsoft Jet database engine. However, the property applies to DAO objects. Therefore, in order to set it from Visual Basic code, you must first create a Property object corresponding to that property and append it to the Properties collection of the DAO object. It's necessary to create the property only the first time you set it.

Sub CheckInherited()
    Dim dbs As Database, tdf As TableDef, qdf As QueryDef
    Dim strSQL As String

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Orders table.
    Set tdf = dbs.TableDefs!Orders
    ' Call SetAccessProperty function.
    If SetAccessProperty(tdf, "DatasheetFontItalic", _
            dbBoolean, True) = True Then
        ' Create QueryDef object based on Orders table.
        strSQL = "SELECT * FROM Orders WHERE ShipCountry = 'USA';"
        Set qdf = dbs.CreateQueryDef("USAOrders", strSQL)
        ' Return Property object pointing to property.
        Debug.Print "Value of Inherited, TableDef object:", _
            tdf.Properties!DatasheetFontItalic.Inherited
        Debug.Print "Value of inherited, QueryDef object:", _
            qdf.Properties!DatasheetFontItalic.Inherited
            ExitCheckInherited
    Else
        MsgBox "Property not set successfully."
        ExitCheckInherited
    End If

ExitCheckInherited:
    Set dbs = Nothing
    Exit Sub
End Sub

This procedure calls the following function, which sets the property, creating it in the Properties collection if necessary:

Function SetAccessProperty(obj As Object, strName As String, _
        intType As Integer, varSetting As Variant) As Boolean
    Dim prp As Property
    Const conPropNotFound As Integer = 3270

    On Error GoTo ErrorSetAccessProperty
    ' Explicitly refer to Properties collection.
    obj.Properties(strName) = varSetting
    obj.Properties.Refresh
    SetAccessProperty = True

ExitSetAccessProperty:
    Exit Function

ErrorSetAccessProperty:
    If Err = conPropNotFound Then
        ' Create property, denote type, and set initial value.
        Set prp = obj.CreateProperty(strName, intType, varSetting)
        ' Append Property object to Properties collection.
        obj.Properties.Append prp
        obj.Properties.Refresh
        SetAccessProperty = True
        Resume ExitSetAccessProperty
    Else
        MsgBox Err & ": " & vbCrLf & Err.Description
        SetAccessProperty = False
        Resume ExitSetAccessProperty
    End If
End Function