VBA ListBox - Selected Value is Returning a Blank - vba

I have a UserForm with some radio buttons and two single column ListBox's. I'm having trouble returning the value from the second ListBox under certain circumstances.
If the user selects one of the radio buttons they get a series of items to select from in the first ListBox. Once they select from the first ListBox, the 2nd ListBox gets populated with items.
If they select the other radio buttons, the two ListBox's just get populated with a single value "Not Applicable" and I'm selecting it straight away.
This is the code I'm using to set the two ListBox's up with "Not Applicable"
ListBox_First.Clear
ListBox_Second.Clear
ListBox_First.List = Array("Not Applicable")
ListBox_First.Selected(0) = True
ListBox_Second.List = Array("Not Applicable")
ListBox_Second.Selected(0) = True
This is my code to get the value selected
Dim firstValue As String
Dim secondValue As String
firstValue = ListBox_First.Value
secondValue = ListBox_Second.Value
firstValue is ok as it equals "Not Applicable", however secondValue is equal to "". When you look at the form, the value in each ListBox looks like it's selected so I don't understand why it's blank. I have checked the ListCount property and each ListBox only has one item so it should be correctly selected.
If I manually select "Not Applicable" in the second ListBox using the mouse is works fine, but I'm trying to avoid the user having to select it when it's the only value.
I don't know if this is a bug or if I've done something wrong with my code.
I'm working with a product called WRQ Reflections.

Caveats of MS Forms Value property
In addition to Dy.Lees valid answer it might be helpful to note that it's preferrable to avoid .Value and to refer e.g. to ListBox2.List(0, 0) instead.
Furthermore note that due to MS help reference at Value property MS Forms
.Value reflects only the "value in the â–ºBoundColumn of the currently selected rows" and that
value cannot be used with a multi-select list box

The value is recognized by adding the setfocus command.
firstValue = ListBox_First.Value
ListBox_Second.SetFocus
secondValue = ListBox_Second.Value

Related

When removing items from a combobox the last item is removed but the text remains... how do I make the combobox blank after last item is removed?

Really simple question but just can't find an answer anywhere.
I've created a command button that removes items from a list in a combobox based on which item they have selected. When the last item is removed, it "technically" is removed but the text for the last item remains in the combobox field. This would confuse the user; in this instance they would believe an item is still attached (the list of items are file pathways.)
I've tried convoluted routes e.g. setting the first index field as "No attachments" and building a solution around this. e.g. when the last field is removed, set it to "no attachments." but this requires me to initialise the combobox this way and change other areas of my code which just unnecessarily complicates it all.
Just a brief description of code, it takes a boolean which the user prompts by choosing either yes to removing all or pressing a remove selected option
Private Sub rmvFile(removeAll As Boolean)
If removeAll = False Then
'trying to work out how to remove selected item
fileListComboBox.RemoveItem (fileListComboBox.ListIndex)
Else
Do While fileListComboBox.ListCount > 0
fileListComboBox.RemoveItem (0)
Loop
End If
End Sub
The result I would expect is that when I remove the last item in the combobox it would be blank, but this doesn't happen. The last items text remains in the combobox, confusing the user.

Listbox; How to search as type, but select top value automatically

When I type into a search box (textbox) my list box will update due to the following criteria within my listbox query;
Like "" & [Forms]![UptimeAARecord]![txtSearchItems].[text] & ""
The list box updates from a OnChange event which the listbox as the user types into the "search" textbox. Everything is working perfectly, however I would like to know how can I make my code select the very top value upon this same OnChange event?
Assuming your listbox is sorted as wanted: This selects the first value.
If Me.myListbox.ListCount > 0 Then
Me.myListbox.Selected(0) = True
End If

Populating a dynamically added listbox from dynamically added combobox selection

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.
OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

How to make a reset button in excel

I am trying to make a reset button that will replace any value the user has selected with the value TOTAL inside a number of comboboxes. Using the record macro i selected the combobox but i can't figure out how to insert the value. The following code gives out the 424 error.
ActiveSheet.Shapes.Range(Array("ComboBox2")).Select.Value = TOTAL
the part that i added to the macro is the .Value=TOTAL
Anyone knows what i should do? Please take note that i don't want to clear the comboboxes; I want to give them a specific value.
In case that combo boxes are Form controls use OLEFormat.Object.ListIndex to select the item.
In case that the combo boxes are ActiveX controls use OLEFormat.Object.Object.ListIndex (note in this case the first item in the list has index 0).
Then iterate through all Combo-boxes you want to reset and set ListIndex to index of item "TOTAL". In this example the item "TOTAL" is on the first place in the list so for Form Combo-box (if used) ListIndex = 1 and for ActiveX Combo-box ListIndex = 0. HTH
(You are probably using ActiveX Combo-boxes, but in the example the older Form Combo-boxes are used as well just for the case).
Sub ResetToTotal2()
Dim combos
Dim combo
Dim comboObject
combos = Array("ComboBox1", "ComboBox2", "ComboBox3")
For Each combo In combos
Set comboObject = ActiveSheet.Shapes(combo).OLEFormat.Object
If TypeName(comboObject) = "DropDown" Then
comboObject.ListIndex = 1
Else
comboObject.Object.ListIndex = 0
End If
Next combo
End Sub

Can't clear a DataGridView combobox without 100s of errors

I'm using VB.NET to make a stand-alone executable containing an unbounded DataGridView.
One of its columns is a combobox. I add rows to the combobox with some simple code
like this:
For r As Int16 = 0 To eachLine.GetUpperBound(0)
Dim dgvcbc As DataGridViewComboBoxColumn = grd.Columns(col)
' Errors: dgvcbc.Items.Clear()
dgvcbc.Items.Add(eachLine(r))
Next r
Works great the first time, but when I try to clear it, in order to add some different
items in the combobox, I get this error 100s of times:
> DataGridViewComboBoxCell value is not valid
Any ideas on how to fix this and why it occurs?
The issue is that, if you clear the Items in that column, every cell in that column becomes invalid. Every value in that column has to match one of the items in the drop-down list and, if there's no items, no value can be valid. It doesn't make sense to clear the drop-down list. You must leave at least the items that match the values in the cells of that column.