CreateProperty Method Example (MDB)

The first procedure in the following example, the SetAccessProperty function, is a generic function that you can use to set a property. If the property you wish to set already has a corresponding Property object in the Properties collection, the function simply sets the property to the desired value. If the property does not have a corresponding Property object in the Properties collection, then the function creates the Property object and appends it to the Properties collection.

The second procedure sets the Subject property using the SetAccessProperty function. The Subject property is a database property that can also be set on the Summary tab of the Database Properties dialog box, available by clicking Database Properties on the File menu.

The Subject property applies to a DAO object  the SummaryInfo Document object, which is defined by Microsoft Access. If the Subject property doesn't already exist in the Properties collection for the SummaryInfo Document object, the SetAccessProperty function adds it.

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

The following procedure calls the SetAccessProperty function to set the Subject property.

Sub CallPropertySet()
    Dim dbs As Database, ctr As Container, doc As Document
    Dim blnReturn As Boolean
    
    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Databases container.
    Set ctr = dbs.Containers!Databases
    ' Return reference to SummaryInfo document.
    Set doc = ctr.Documents!SummaryInfo
    blnReturn = SetAccessProperty(doc, _
        "Subject", dbText, "Business Contacts")
    ' Evaluate return value.
    If blnReturn = True Then
        Debug.Print "Property set successfully."
    Else
        Debug.Print "Property not set successfully."
    End If
End Sub