right click menu in popup form - ms-access

I'm new to this great forum.
I'm trying to do right click menu on listbox.
currently i'm trying to implement a right click menu which will show a simple messege box.
My problem is that the list box is on a pop up form which opened up maximized. Now when i'm right clicking on the list box i see the right click menu but when i'm clicking on one of the menu options, nothing happenning (it seems that it don't go to the function as it should). i've also putted breakpoints on the function but it never tips.
It's important to mention that if i'm setting the form popup option to no the right click menu works perfectly (when i'm clicking on one of the options i see its matching messege box).
I'm running the following vba code:
This is the mouse up event handler for my list box:
Private Sub Song_List_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Call the SetUpContextMenu function to ensure it is setup with most current context
' Note: This really only needs to be setup once for this example since nothing is
' changed contextually here, but it could be further expanded to accomplish this
SetUpContextMenu
' See if the right mouse button was clicked
If Button = acRightButton Then
'DoCmd.CancelEvent
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub
setting up the "SetUpContextMenu" sub:
Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarControl
' Since it may have been defined in the past, it should be deleted,
' or if it has not been defined in the past, the error should be ignored
On Error Resume Next
CommandBars("MyListControlContextMenu").Delete
On Error GoTo 0
' Make this menu a popup menu
With CommandBars.Add(Name:="MyListControlContextMenu", Position:=msoBarPopup)
' Provide the user the ability to input text using the msoControlEdit type
Set combo = .Controls.Add(Type:=msoControlEdit)
combo.Caption = "Lookup Text:" ' Add a label the user will see
combo.OnAction = "=getText()" ' Add the name of a function to call
' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.BeginGroup = True ' Add a line to separate above group
combo.Caption = "Lookup Details" ' Add label the user will see
combo.OnAction = "=LookupDetailsFunction()" ' Add the name of a function to call
' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.Caption = "Delete Record" ' Add a label the user will see
combo.OnAction = "=DeleteRecordFunction()" ' Add the name of the function to call"
combo.SetFocus
combo.Visible = True
End With
End Sub
setting up the all 3 function which shows different messege boxes on click:
Public Function getText() As String
getText = CommandBars("MyListControlContextMenu").Controls(" Lookup Text:").Text
' You could optionally do something with this text here,
' such as pass it into another function ...
MsgBox "You typed the following text into the menu: " & getText
End Function
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
Public Function DeleteRecordFunction()
' If Not IsNull(Forms!MyFormName.Controls("Song_List").Colu mn(0)) Then
MsgBox "Record Deleted"
' End If
End Function

Just set the Shortcut menu bar setting in the property sheet (in the other tab) to the name of your shortcut menu.
You at that point should not need ANY code to display/launch the context menu you created.

Related

AHK V2 How to call a function on the click event for a button

I want to call a function whenever a button on my Gui is pressed, the code is as follows:
The ultimate idea of this is to open the gui when a window appears
Add Check Boxes
Add Button
User Checks Boxes and clicks button
text in array for checkboxes in array gets typed out int a field in the window.
`
MyGui:= Gui()
myGui.Title := "Test"; ;Gui Name
MyBtn_Click() ;Function to be called when button pressed
{
msgBox("Pressed Button")
}
myBtn := MyGui.AddButton(,"OOK") ;Adds an ok Button to the Gui
myBtn.OnEvent("Click", MyBtn_Click(*)) ;Call the function
myGui.Show() ;Shows Gui
`
Now the problem is when I try to call the function my button click it gives me a syntax error. I've tried to make sense of how to define a function and how to call it, but I cannot figure it out.
You need to put the asterisk in the function definition (see variadic functions) and then remove the parentheses in the second OnEvent parameter.
#Requires AutoHotkey v2.0
MyGui:= Gui()
myGui.Title := "Test" ;Gui Name
MyBtn_Click(*) ;Function to be called when button pressed
{
msgBox("Pressed Button")
}
myBtn := MyGui.AddButton(,"OOK") ;Adds an ok Button to the Gui
myBtn.OnEvent("Click", MyBtn_Click) ;Call the function
myGui.Show() ;Shows Gui

use ms-access autokeys macro to toggle main menu, ribbon and navigation pane

Trying to add this function to the Autokeys macro to toggle off main menu, ribbon and navigation pane:
Public Function nomenu ()
DoCmd.NavigateTo "acNavigationCategoryObjectType"
DoCmd.RunCommand acCmdWindowHide
DoCmd.ShowToolbar "Ribbon",acToolbarNo
End Function
The Autokeys macro is set up as:
Submacro: ^z
RunCode
Function Name nomenu ()
End Submacro
Want to add this function plus another to have two "hot keys" to toggle on and off the menus and navigation pane.
Get following error message:
"The expression you entered has a function name that (DB name) can't find." Error number 2425.

How to set the cursor to the end of the text box without losing the setfocus?

I am using this code to set the focus on the end of the text box in a form but I can only do so if I set the focus to that text box which is at the middle of my form. I have setup tab control and I want the focus to be at the first text box.
My code is:
Me.txtBox.Locked = True
Me.txtBox.SetFocus
Me.txtBox.SelStart = Len(Me.txtBox.Text)
I tried add this after the code above to set the focus to first text box again.
pfPositionCursor Me.txtBox1
But I get sub or function undefined on pfPositionCursor
Is there a way to do something like this?
You can pass a control in a function for example:
Call ControlFocusFunction(Me!MyTextbox)
Public Function TextBoxFocusFunction(ctlControl as Control)
If ctlControl.ControlType = acTextBox Then
ctlControl.SetFocus
ctlControl.SelStart(Len(ctlControl.Text))
End If
End Function

Ms-Access - IF checkbox = true THEN

How would i make it so that when 4 check boxs are all ticked (true) then the button may then be pressed to save the record.
The four checkbox s are called "chk1, chk2 chk3, chk4"
button is called "button"
i presume that it would something along the lines of "If Chk1, chk2, chk3, ck4 = True Then Button = visible?"
and then if the check boxes are not true then the button is not able to be pressed
You create a function that checks the checkboxes and sets the button state accordingly...
Function CheckMyButton()
Me.MyButton.Enabled = (Me.chk1 And Me.chk2 And Me.chk3 And Me.chk4)
' If you want to be more verbose, do
' (Me.chk1.Value=True And Me.chk2.Value=True And ...
End Function
... and call this function from the After Update event of all checkboxes.
Edit and probably in the Form_Current event too, if it's a bound form.
You can directly use
=CheckMyButton()
in the event property, no need for 4 event procedures.

jpopupmenu where to add listener

I have a jTextField with a button along side to call a popupmenu. The popupmenu contains a list of standard text for the jTextField. This list is held in a list array of variable length, since it can be added too elsewhere in the program.
I'm using the following to generate the popupmenu.
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
}
My question is how do I include the listener that captures which item on the popup menu is selected so I can then put the value into the jTextField.
You need to add a ItemListener interface. If your JMenuItem implements ItemSelectable interface. You can change your code to be like below:
ItemListener il =
e -> {JMenuItem source = (JMenuItem)(e.getSource());
String s = "Item event detected on '" + source.getText() +",New state: "
+ ((e.getStateChange() == ItemEvent.SELECTED) ?
"selected":"unselected");
jTextField.setText(s);
};
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
item.addItemListener(il);
}
More examples here and here