DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 16.3 Display Smart Tags when Application Starts

16.3.1 Problem

My application makes extensive use of smart tags. Since this is a global setting, some users may have turned off the display of smart tags. How can I ensure that smart tags are displayed when my application starts up?

16.3.2 Solution

The display of smart tags is controlled by a checkbox in the Tools Options dialog box under the Forms/Reports tab. These settings apply to Access as a whole, and once changed, take effect for all running applications. Figure 16-5 shows the dialog box with the display of smart tags turned on.

Figure 16-5. Setting smart tag viewing options
figs/acb2_1605.gif

The best place to set options for your application is in a startup form, which can run code to ensure that settings are the way you need them to be. The sample application is configured to use a form named frmStartup, by setting the Display/Form Start Page property in the Tools Startup dialog box, as shown in Figure 16-6.

Figure 16-6. Configuring a startup form
figs/acb2_1606.gif

Follow these steps in your startup form to ensure that smart tags are displayed for your application:

  1. Create a variable in the declarations section of the form that you will use to retrieve and store the user's current settings. Options controlled by checkboxes return either True or False. However, retrieving and setting options in code requires the Variant data type, not a Boolean, as you might expect.

    Option Explicit
    
    Private varSmartTagOn As Variant
  2. In the form's Open event, retrieve the user's current settings using the GetOption method. If smart tags are not turned on, then use SetOption to turn them on. The sample application collects the user's name using the InputBox function and displays it in a label control that has the Person Name smart tag attached:

    Private Sub Form_Open(Cancel As Integer)
        ' Retrieve user's SmartTag settings
        varSmartTagOn = Application.GetOption( _
         "Show Smart Tags on Forms")
         
        ' Display smart tags if needed
        If Not varSmartTagOn Then
            SetOption "Show Smart Tags on Forms", True
        End If
        
        lblName.Caption = InputBox( _
         "Type your name:", "Welcome Message", "")
    End Sub
  3. In the form's Close event, reset the user's smart tag options to their original values:

    Private Sub Form_Close( )
        ' Restore user's smart tag option setting
         Application.SetOption "Show Smart Tags on Forms", varSmartTagOn
    End Sub

16.3.3 Discussion

By saving the smart tag settings in a variable, you can ensure that your application behaves in a polite way, only changing the user's global settings that are needed for your application to function properly. In the sample application, the code in the Close button event handler closes the form and resets the user's smart tag options to whatever they were when the application opened. You could elect to simply hide the form instead:

Private Sub cmdClose_Click( )
    Me.Visible = False
End Sub

The code in the form's Close event will not execute if the form is hidden and not closed. When the application shuts down, the form closes and the code in its Close event runs and resets the user's smart tag options to their original values.

The frmStartup form in the sample database also contains a Toggle Smart Tags button that toggles the display of the smart tags option. The ToggleShowSmartTags procedure reverses the current option settings for displaying smart tags and stores the new setting in the varSmartTagOn variable:

Private Sub ToggleShowSmartTags( )
    ' Toggle smart tag settings
    varSmartTagOn = Not varSmartTagOn
    Application.SetOption "Show Smart Tags on Forms", varSmartTagOn
    MsgBox "Application Settings = " & varSmartTagOn, , "Show Smart Tags"
End Sub

You can test the code by opening frmStartup and frmTest side by side. You can see the smart tags on both forms enabled or disabled as you click the Toggle Smart Tags button on frmStartup.

    [ Team LiB ] Previous Section Next Section