VBA - Create a menu list when you right click a listbox - vba

I would like to have a menu with two options when user right clicks listbox.
With that menu user would do some actions on selected data, e.g. add new line, change some columns data or delete row. I think I can manage that code but I can not grasp how to create a right click custom menu.
I can not find a way to create such option. Does anyone has an idea?

Here's some code for the right click event:
Select ListBox item on rightclick in Word VBA
Replace this code with your context menu code:
Me.Caption = derivedIndex & " = " & ListBox1.List(derivedIndex)
Here's a example for the popup menu (you need skills to understand Win32 API calls...)
https://www.vbarchiv.net/api/api_createpopupmenu.html

The right click menu on the listbox that created by using only a class(without using Api or Declare function) contains the following buttons ;
Increase Font Size
Decrease Font Size
Sort A-Z
Remove List
Remove All
For VBA codes and sample file review this link

Related

Select all text without ActiveX buttons

I am trying to build a template that has ActiveX buttons for calling Sub procedures. At some point, I have to select the text and convert it to a table.
So, I have to find a way to select the text, without selecting the ActiveX controls.
I've figured there would be two approaches:
write a code that selects the text without objects (I only have
ActiveX objects);
change the objects property in a way that they don't get
selected using Selection.WholeStory or similar code.
I've tried the following methods, without success:
using Selection.WholeStory - it selects all text, including the ActiveX controls
wraping the objects Square, not in line with the text, and positioning them outside the printing area - the objects still get selected
putting the objects in the header - in this way, the buttons cannot be used/clicked
putting the controls in a textbox - they still get selected
Do you know a way of achieving my purpose?
The easiest way was to put a section marker after the buttons (that are configured as "in line") and protect first section from editing.
Click on Developer -> Restrict Editing -> Check Allow only this type of editing in the document and Select Filling in forms. Then, click on Select Sections and Select the first section.
Next, in the VBA code, to select all the text, I use this code:
Dim rngText As Range
Set rngText = ActiveDocument.Sections(2).Range
rngText.Select

Does Excel VBA object model allows for access to keyboard shortcut to VBA IDE menu items?

Does Excel VBA object model allows for access to keyboard shortcuts to VBA IDE menu items ?
(I would like to create additional item in menu and connect it with choosen shortcut)
For example code below returns "C&lear" which means you can access "Clear" item by typing "l" when the "Edit" group in VBE IDE is active, but you can also access "Clear" by typing "Delete" button alone :
Debug.Print Application.VBE.CommandBars(1).Controls(2).Controls(6).Caption
is there a way to find pragrammaticaly direct shortcut to item in IDE menu ("Delete" in the case above) ? I would like to add item to VBA IDE menu with new custom shortcut ?
Debug.Print Application.VBE.CommandBars(1).Controls(3).Controls(6).Caption
returns "&Immediate Window" but you could also access Immediate Window by direct shortcut "Ctrl+G"
is there a way to find pragrammaticaly direct shortcut to item in IDE menu ("Delete" in the case above)
Yes, there is. Try this
Debug.Print Application.VBE.CommandBars(1).Controls(2).Controls(6).ShortcutText
This will give you Del
Followup from comments:
Dim b As CommandBarButton
Set b = Application.VBE.CommandBars(1).Controls(2).Controls(6)
Debug.Print b.TooltipText
returns:
C&lear (Del)
Similarly,
Set b = Application.VBE.CommandBars(1).Controls(2).Controls(6)
Debug.Print b.TooltipText
returns
Export (Ctrl+E)

How to set textboxes in order in VB?

I want to make textboxes and other input values in order, so I can move from one to another with "Tab" button.
At the moment I have 3 textboxes for date input and when I run the program it always set my cursor to 3rd textbox by default instead of 1st.
In Visual Studio, you'll want to set the Tab Order.
To do this, when in the Form Designer, go to the View Menu, then select "Tab Order"
You'll get a little index on all your controls. Simply click the controls in the order you want to move through them with the Tab Key and you'll be set. Be sure to turn off the Tab Order when you're done by going back to the View Menu and de-selecting Tab Order.
Good luck.
look into TabIndex and also, through your menu (depending on your VS version - i'm using 2010) - use View > Tab Order

Visual Basic: How to replace selected NumericUpDowns with ComboBoxes

Is it possible to replace the selected NumericUpDown controls with ComboBoxes?
I understand that not all properties can be conserved but the only properties I need are the location and the size.
The workflow I have in mind is as follows:
Select certain NumericUpDowns
Click replace with... and then select ComboBoxes (or any other approach)
Where the NumericUpDowns were, there are now ComboBoxes of the same size
The reason I want to do this is that I have to put together a GUI with multiple tabs. Each tab page has a list of Labels with either NumericUpDowns or ControlBoxes next to it. The order of the controls changes per tab. I just want to copy the items on the first tab and paste them on the other tabs. Then per tab I only have to change certain NumericUpDowns into ComboBoxes.
I started with VB yesterday so I might be overlooking something.
The quickest is doing it manually, we cant change your GUI remotely - since you are at an entry level with a language you do not know well, RAD is the best recommendation - this way you can study what it does - just like learning HTML with DreamWeaver's RAD tools.
Since your interested, (I know your new to VB so I'dd make it heaps clear) you do these steps:
a) Open Winforms VS 2008 solution
b) Click the File > Create New Project > WinForms
c) Double click the form and it will show you the forms code
d) Then in the constructor method you will see the line InitializeComponent
e) Right Click on this method call and chose Goto Definition
f) This will show you the code that populates the form with controls
g) Then for each form I'm suggestion you replace all the NumericUpDown's with ComboBoxes in the xyz.Designer.vb file
However I'd really recommend doing it with Visual Studio IDE. Don't be frightened.
Private sub Replace_By_ComboBox(ByVal nud As NumericUpDowns)
'Create new combo box
Dim cbx As New ComboBox
cbx.Left = nud.Left
cbx.To = nud.Top
cbx.Width = nud.Width
cbx.Height = nud.Height
cbx.Visible = True
cbx.Enabled = True
'Add the combo box
nud.Parent.Controls.Add(cbx)
'Remove the NumericUpDowns
nud.Parent.Controls.Remove(nud)
End Sub

Need to build a macro that will take value(table name/form name) from a textbox and open the related table/form in ACCESS

i want to create a starting form in ACCESS that will hold a Textbox and a button.If user input text(say form name/table name) and click the button,it will show the form/table.If the name is not correct it will show a message.I am new in access.please help me with the macro or other things.
Thanks in advance.
Create a table and put the choices.
Create a blank form in design view.
In this blank form add a text box and a button.
Right click the text box, then choose Change To Combo Box
View the Combo Box properties
From the combo box properties, go to Data Tab
Here, you will see Row Source. Click the drop down in the Row Source and select the table where you put your choices.
You can Limit users to choose only from the available choices by changing Limit to List from No to Yes.
If you will limit your users from the choices, better not to add the button.