Solved

MS Access Listbox Context Menu

Posted on 2012-12-21
7
1,001 Views
Last Modified: 2012-12-24
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.
0
Comment
Question by:lrbrister
7 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 38713977
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.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
ID: 38713992
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
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38714044
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
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 75
ID: 38714105
"Not sure about right-clicks in Access 2007/2010."

?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38714285
Right clicks (shortcut menus) are not a problem in 2007, but have not tested with a 2010 listbox.
0
 
LVL 75
ID: 38714313
All MenuBars (Menus, Toolbars & Right Click) port over to A2010 from A2003.

mx
0
 

Author Closing Comment

by:lrbrister
ID: 38718466
Sorry for late get back.

Tested both answers.

Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now