ACC2: Simple Method for Creating ToolTips for Form Controls

ID: Q132379


The information in this article applies to:


SUMMARY

This article describes a method that you can use to add custom ToolTips (brief descriptions that appear when the mouse pointer moves over particular controls) to forms.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Access Basic, please refer to the "Building Applications" manual.


MORE INFORMATION

This method creates ToolTips by using a text box, the Tag property of form controls, and a custom function that is triggered by the MouseMove event of form sections and controls. For example, when you move the mouse pointer over a particular control, the MouseMove event triggers the custom function. The function moves a text box displaying a brief description stored in the control's Tag property under the control.

This method uses a single text box to display the ToolTip messages for all the controls. Be aware, however, that the size of the text box is not adjusted for descriptions of varying lengths. To avoid cutting off the ToolTip text, use a set number of characters for the control descriptions.

To add custom ToolTips to a form, follow these steps.

  1. Open the sample database NWIND.MDB, and open the Customers form in Design view.


  2. Add a text box to the Customers form and set the text box control's properties as follows:
    
          Name: ToolTip
          Visible: No
          BackColor: 8454143 

    NOTE: You do not need the label for this control; you can delete it.


  3. Add a command button to the Customers form and set the command button control's properties as follows:
    
          Name: MyButton.
          Tag: My Button
          OnMouseMove: =ShowToolTip("MyButton") 


  4. On the View menu, click Code and type the following function in the Module window:
    
           Function HideToolTip()
              Dim z As Integer
              On Error Resume Next
              If Me!ToolTip.Visible = True Then
                  Me!ToolTip.Visible = False  'Turn off the ToolTip.
                  z = SysCmd(SYSCMD_CLEARSTATUS)
              End If
           End Function 


  5. Set the Customers form detail section's OnMouseMove property as follows:
    
          =HideToolTip() 

    Note: This procedure must be called from the MouseMove event of any section where you use a control with a ToolTip.


  6. Close and save the Customers form.


  7. Create a new module and type the following line in the Declarations section:
    
          Option Explicit 


  8. Type the following function:
    
           Function ShowToolTip (ShowControl As String)
              Dim MyControl As Control
              Dim MyToolTip As Control
              Dim z as Integer
              Const Separator = 80
              On Error Resume Next
    
              Set MyControl = Screen.ActiveForm(ShowControl)
              Set MyToolTip = Screen.ActiveForm!ToolTip
    
              If MyToolTip.Visible = False Then
                  MyToolTip = MyControl.Tag
                  MyToolTip.Left = MyControl.Left + (Separator * 2)
                  MyToolTip.Top = MyControl.Top + MyControl.Height + Separator
                  MyToolTip.Visible = True
    
                  ' Optional: Display ToolTip on the Status Bar.
                  z = SysCmd(SYSCMD_SETSTATUS, MyToolTip.value)
              End If
    
           End Function 


  9. Open the Customers form in Form view and move the mouse pointer over the command button to display the ToolTip message under the button.


NOTE: The MouseMove event of the detail section may not run and the ToolTip may remain visible if you rapidly move the mouse pointer to a control without a ToolTip. To ensure that the ToolTip turns off, call the same function from Step 6 on the MouseMove event of controls that do not display a ToolTip.


REFERENCES

For more information about creating ToolTips using Windows Application Programming Interface (API) functions, please see the following article in the Microsoft Knowledge Base:

Q119991 ACC2: How to Add ToolTips to Form Controls

Additional query words: tool tips


Keywords          : kbusage FmsHowto 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 28, 1999