USING CUSTOM PROPERTIES

Access uses DAO to track different pieces of information about your application. For example, the application title, application icon, summary information about the database, and much more information is stored in DAO.

To view your application title, icon, and other information about the application, choose Startup from the Tools menu. The dialog shown in Figure 5.6 appears.

Figure 5.6. The Startup dialog lets you define the startup options for your application, such as the initial form to open and what menu bars to display.


Access stores the information in this dialog in the database by using custom properties. However, unless you've set the property through the user interface, the property may not yet be added to the database. Therefore, whenever you write code that sets or retrieves a custom property, you must first check to see whether the property exists.

Listing 5.11 shows the sample code that attempts to set a property. This code takes the object the property resides on, the property name, and the value to assign to the property. If the property is successfully set, True is returned; otherwise, False is returned.

Note

Sometimes it might be better for the application to store custom properties in the CurrentProject object or its collections, especially if the database might be upsized. To learn more about CurrentProject, see Chapter 4.


Listing 5.11. Chap05.mdb: Assigning Values by Using the Properties Collection
Function AssignProperty(objSource As Object, prpName As String, _
         prpType As Variant, prpValue As Variant) As Boolean

     Dim prp As Property
     On Error GoTo Assign_Err
     objSource.Properties(prpName) = prpValue
     AssignProperty = True

AddProp_Exit:
     Exit Function
Assign_Err:
     AssignProperty = False

End Function

You can use the code in Listing 5.12 to add a property to an object in DAO. The function takes the DAO object to add the property to, the name of the property, the data type, and an optional value to assign to the property.

Listing 5.12. Chap05.mdb: Adding a Custom Property to a DAO Object
Function AddProperty(objSource As Object, strName As String, _
         varType As Variant, Optional varValue As Variant) As Boolean

     Dim prpProp As Property

     On Error GoTo Err_AddProp
     If Not IsMissing(varValue) Then
       Set prpProp = objSource.CreateProperty(strName, varType, varValue)
     Else
       Set prpProp = objSource.CreateProperty(strName, varType)
     End If
     objSource.Properties.Append prpProp

     AddProperty = True
Exit_AddProp:
     Exit Function
Err_AddProp:
     AddProperty = False
     Resume Exit_AddProp

End Function

Not all objects in DAO support defining custom properties. The objects in DAO that support user-defined properties are as follows:

  • Database

  • Index

  • QueryDef

  • Field (in TableDef and QueryDef)

  • Document

In the database container's Documents collection, document is a UserDefined Document object. The UserDefined Document is an object in the Documents collection where you can store application attributes. For example, in VideoApp.mdb, the UserDefined document is used to store the name of the back-end database. To access the number of properties on the UserDefined object, in the Immediate window, you can write the following line of code:

? CurrentDb().Containers!Databases.Documents
("UserDefined").Properties.Count

Any major item of information that you want to store for the database itself can be stored in the UserDefined document.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.69.244