MS Access Listbox Context Menu

Larry Brister
Larry Brister used Ask the Experts™
on
On my listbox I would like to add a function to the Right Mouse Click.

Essentially I was it to say..."Remove Client from Visit"

On click run a Sub.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
You can create your own shortcut menu and assign that to the listbox ShortCutMenu property.  The following is a sample code segment:
Public Sub MenuClientVisit()

    Dim cbr As Object
    Dim cbrButton As Object

    On Error GoTo ProcError
    
    Set cbr = CommandBars.Add("ClientVisitShortcut", BarPopup, , True)

    With cbr

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "&Remove Client from Visit"
            .Tag = "RemoveClientVisit"
            .OnAction = "=fnRemoveClientVisit()"
        End With

    End With

ProcExit:
    Exit Sub
ProcError:
    msgbox err.number & vbcrlf & err.desription, vbOKONLY, "ClientVisitMenu error"
    
End Sub

Open in new window

This code will create a commandbar named ClientVisitShortcut with a single item "Remove Client from Visit".  The key is that you then need to write the code for the action which will be taken when you actually select the "Remove Client from Visit" option.  This code must be in the form of a function (not a subroutine), but it does not have to return a value, and in this example, the function name would be "fnRemoveClientVisit"; and it must be placed in a public code module (not in a forms code module).

I couple this code with a subroutine which returns a boolean value that indicates whether the commandbar already exists.
Public Function CmdBarExists(BarName As String) As Boolean

    Dim intControls
    
    On Error Resume Next
    intControls = CommandBars(BarName).Controls.Count
    If Err.Number = 0 Then
        CmdBarExists = True
    Else
        CmdBarExists = False
    End If
    
End Function

Open in new window

Then, on the form where this listbox is located, add the following code to the Open event of the form.
Private Sub Form_Open

    'Check to see if the commandbar already exists, and respond appropriately
    If CmdBarExists("ClientVisitShortcut") = false Then Call MenuClientVisit
    me.lst_ClientVisits.ShortcutMenuBar = "ClientVisitShortcut"

End Sub

Open in new window

This will cause your application to check to see whether the commandbar already exists, and if not, it will create it.  It then assigns the ClientVisitShortcut commandbar to the rightclick event of lst_ClientVisits.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007
Commented:
No code version ... 5 minutes to create:

Easily create real, professional Windows menus, toolbars and Right Click menus.

This works in the A2003 (and prior) environment and MDB format.  And they will port (with MDB) into A2007/10 environment and show up on the Add-Ins Ribbon.

Everything you need is here - no code ... see this link:

http://www.jamiessoftware.tk/articles/menubars.html
       

mx
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
Not sure about right-clicks in Access 2007/2010.

Why not just put this on a button?
So the user could just select the Record from the listbox, then click the button to "remove"...

The code would be something like this regardless:

Dim strSQL as string
strSQL ="DELETE * FROM YourTable WHERE ClientID=" & lstClient
Currentdb.execute strSQL, dbfailonerror
me.lstClient.requery


JeffCoachman
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
"Not sure about right-clicks in Access 2007/2010."

?
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
Right clicks (shortcut menus) are not a problem in 2007, but have not tested with a 2010 listbox.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
All MenuBars (Menus, Toolbars & Right Click) port over to A2010 from A2003.

mx
Larry Bristersr. Developer

Author

Commented:
Sorry for late get back.

Tested both answers.

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial