Solved

MS Access Listbox Context Menu

Posted on 2012-12-21
7
958 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"Not sure about right-clicks in Access 2007/2010."

?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Right clicks (shortcut menus) are not a problem in 2007, but have not tested with a 2010 listbox.
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
All MenuBars (Menus, Toolbars & Right Click) port over to A2010 from A2003.

mx
0
 

Author Closing Comment

by:lrbrister
Comment Utility
Sorry for late get back.

Tested both answers.

Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

771 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

11 Experts available now in Live!

Get 1:1 Help Now