Application Object.
Application.GetOption(optionname)Application.SetOption optionname, setting
The GetOption and SetOption methods use the following arguments.
Argument |
Description |
optionname |
The name of the option. |
setting |
A Variant value corresponding to the option setting. The value of setting depends on the possible settings for a particular option. |
The GetOption and SetOption methods provide a means of changing environment options from Visual Basic code. With these methods, you can set or read any option available in the Options dialog box, except for options on the Modules tab.
The option setting depends on the type of option being set. There are three general types of options:
For options that the user sets by selecting or clearing a check box, the GetOption method returns True if the option setting is Yes (the check box is selected) or False if the option setting is No (the check box is cleared). To set an option of this kind using the SetOption method, specify True or False for the setting argument, as in the following example.
Application.SetOption "Show Status Bar", True
For options that the user sets by typing a string or numeric value, the GetOption method returns the setting as it is displayed in the dialog box. The following example returns a string containing the left margin setting.
Dim varX As Variant= Application.GetOption("Left Margin")
To set this type of option using the SetOption method, specify the string or numeric value that would be typed in the dialog box. The following example sets the default form template to OrderTemplate.
Application.SetOption "Form Template", "OrderTemplate"
For options with settings that are choices in list boxes, the GetOption method returns a number corresponding to the position of the setting in the list. Indexing begins with zero, so the GetOption method returns zero for the first item, 1 for the second item, and so on. For example, if the Default Field Type option on the Tables/Queries tab is set to AutoNumber, the sixth item in the list, the GetOption method returns 5.
To set this type of option, specify the option’s numeric position within the list as an argument to the SetOption method. The following example sets the Default Field Type option to AutoNumber.
Application.SetOption "Default Field Type", 5
Other options are set by clicking on an option button in an option group in the Options dialog box. In Visual Basic, these options are also set by specifying a particular option’s position within the option group. The first option in the group is numbered zero, the second, 1, and so on. For example, if the Selection Behavior option on the Forms/Reports tab is set to Partially Enclosed, the GetOption method returns zero.
Debug.Print Application.GetOption("Selection Behavior")
To set an option that is a member of an option group, specify the index number of the option within the group. The following example sets Selection Behavior to Fully Enclosed.
Application.SetOption "Selection Behavior", 1
Note When you use the GetOption method or the SetOption method to set an option in the Options dialog box, you don’t need to specify the individual tab on which the option is found.
You can’t use the GetOption method or the SetOption method to read or set any of the options found on the Module tab of the Options dialog box.
If the return value of the GetOption method is assigned to a variable, the variable must be declared as a Variant.
When you quit Microsoft Access, you can reset all options to their original settings by using the SetOption method on all changed options. You may want to create public variables to store the values of the original settings. You might include code to reset options in the Close event procedure for a form, or in a custom exit procedure that the user must run to quit the application.
The following example uses the GetOption method to return the Default Field Type option setting from the Tables/Queries tab of the Options dialog box and then saves the setting in a variable. If the Default Field Type option isn’t currently set to Text, Microsoft Access displays a dialog box asking the user whether it should set the option to Text. If the user clicks Yes, the SetOption method changes the option.
Sub ChangeFieldSize() ' Constant represents Text setting of Default Field Type option. Const conText = 0 Dim varSize As Variant, intResponse As Integer, strMsg As String ' Determine current setting. varSize = Application.GetOption("Default Field Type") strMsg = "Set default field type to Text?" If varSize <> conText Then ' Prompt user to change setting if it's not Text. If MsgBox(strMsg, vbYesNo) = vbYes Then ' Change setting to Text. Application.SetOption "Default Field Type", conText End If End IfSub