Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


926


Setting property values
You use the Application object’s SetOption method to set each of these properties, and the
GetOption method to retrieve the current value. The syntax of the SetOption method is

Application.SetOption OptionName, Setting

where OptionName is the name of an option in Table 26.4, and Setting is one of a number of differ-
ent data types, depending on the option being manipulated with SetOption.

In most cases, unless the property has already been set in the Access Options dialog box, the prop-
erty hasn’t been appended to the Application object’s Properties collection. You must make
sure the property exists before trying to set its value in code. The following function sets the value
of a start-up property, creating and appending the property to the Application object’s
Properties collection if the property doesn’t exist:

Function AddStartupProperty(PropName As String, _
PropType As Variant, PropValue As Variant) _
As Integer
‘Consult the Access online help for the PropName
‘and PropType for each of the startup options.
‘Adding a property requires the appropriate
‘PropType variable or the property creation fails.
Dim MyDB As DAO.Database
Dim MyProperty As DAO.Property
Const _PropNotFoundError = 3270
Set MyDB = CurrentDB
On Error GoTo AddStartupProp_Err
‘The following statement will fail if the
‘ property named PropName doesn’t exist.
MyDB.Properties(PropName) = PropValue
AddStartupProperty = True
AddStartupProp_OK:
Exit Function
AddStartupProp_Err:
‘Get here if property doesn’t exist.
If Err = _PropNotFoundError Then
‘Create the new property and set it to PropValue
Set MyProperty = MyDB.CreateProperty(PropName, _
PropType, PropValue)
‘You must append the new property
‘to the Properties collection.
MyDB.Properties.Append MyProperty
Resume
Else
‘Can’t add new property, so quit
AddStartupProperty = False
Resume AddStartupProp_OK
End If
End Function ‘AddStartupProperty
Free download pdf