ACC2000: How to Create Floating Pop-Up Menus (210093)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210093
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to use a combination of Microsoft Windows application programming interface (API) calls and Visual Basic for Applications code to create floating pop-up menus.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To create a floating pop-up menu, follow these steps:
  1. Start Microsoft Access and open any database.
  2. Create a new form called Pop-up Menu Form.
  3. Add a list box control called Menu to the form. Make the control large enough so that it is able to display each of the strings it will contain without displaying a scroll bar. Set the control's properties as follows:

    Name: Menu
    RowSourceType: Value List
    RowSource: String1;String2;String3;String4
    AfterUpdate: =ItemSelected([Menu])
    Left: 0 in
    Top: 0 in
    FontName: System
    FontSize: 8
    FontWeight: Bold

    NOTE: Specify the menu strings in the RowSource property. Separate the string values with a semicolon (;).

  4. Set the properties of the Pop-up Menu Form form as follows:

    ScrollBars: Neither
    RecordSelectors: No
    NavigationButtons No
    PopUp: Yes
    Width: Width of the list box control

  5. Set the Height property of the form's Detail section to match the height of the list box.
  6. Save the form and close it.
  7. Create a new module, and then type or paste the following code in the Declarations section of the module:
    ' NOTE: Some of the following Windows API functions may be
    ' defined in an existing Microsoft Access library. If so, the new
    ' declarations would cause a duplication procedure name error. If
    ' this error occurs, remove the offending declare statement from
    ' your code or convert the declaration to a comment.
    
    Option Explicit
    Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Global Const GWL_STYLE = (-16)
    Global Const WS_DLGFRAME = &H400000
    
    
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) _
         As Long
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong _
        As  Long) As Long
    					
  8. Type or paste the following functions:
    Function ShowPopup ()
       Dim coord As POINTAPI
       Dim attr&
    
       GetCursorPos coord
       DoCmd.OpenForm "Pop-up Menu Form"
    
       attr& = GetWindowLong(Forms![Pop-up Menu Form].hWnd, GWL_STYLE)
       attr& = SetWindowLong(Forms![Pop-up Menu Form].hWnd, GWL_STYLE, _
                  attr& And Not WS_DLGFRAME)
    
       DoCmd.MoveSize (coord.x * 14), (coord.y * 14), , 1100
    End Function
    
    Function ItemSelected (WhichItem As String)
       DoCmd.Close
       MsgBox "The selected item was " & Trim(WhichItem)
    End Function
    					
  9. Compile the module by clicking Compile database on the Debug menu.
  10. Select the form in which you want the pop-up menu to appear. Open the form in Design view.
  11. Select the event property that you want to use to open the pop-up menu, and then specify the following function call:

    =ShowPopup()

    NOTE: To better see how this step works, you may want to specify the function for the DblClick event of a text box.
When you open the form and cause the event assigned in step 11 to run, the pop-up menu appears at the current mouse position. The menu remains on the screen until you select an item from the pop-up menu.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbinfo kbProgramming KB210093