How to set the items of a ComboBox into another one - vba

I'm populating a VBA ComboBox with some data from a Workshet. As it is populated depending on the choice of the user, sometimes some levels are should not be populated, because they do not have data. When such a thing happens, I want to populate the items of a ComboBox with the same of another. How can a do that?
I think if somebody teaches me how to loop through the items of a ComboBox, I will the able to do what I want.

Here is how you loop through a combobox.
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
MsgBox Me.ComboBox1.List(intComboItem)
Next

Related

Adding unique records to a listbox from another listbox

I am hoping someone can help me out because I don't often code in Excel VBA. I have two Listbox and a CommandButton on a UserForm, which adds selected records from the first ListBox to the second. The range in the first ListBox is kind of long and so users of the form will generally select a couple records, hit the Add button, and then scroll down the first ListBox to select more records to add. The problem is, only the ones that have been added last will actually be recognized and used to generate reports even though all of the selected records appear in the second ListBox. Here is my current code for the button that adds the records to the second ListBox:
Private Sub AddButton_Click()
For i = 0 To Listbox1.ListCount - 1
If Listbox1.Selected(i) = True Then Listbox2.AddItem Listbox2.List(i)
Next i
End Sub
How can I populate the second ListBox so that it will include every record selected no matter how many times the add button is used?

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

Multi-Criteria Search via three comboxes in vb.net with sql back end

I am currently working in VB.net with an sql back end server. I have a windows form application with a datagridview and 3 combo boxes that have data from three different columns in the datagridview. So far I have used IF statements for multi-criteria searches. This method has proven to be a long and inefficient way to write this code. It is literally a long run of IF statements saying if combobox1.selectedindex does not equal 1 and combobox 2 and 3 are still on the -1 index then search with just the first combo box. Then I write the code for every single possibility of the comboboxes being filled out. Whether it is 2 or all of them, or just one of them. I am wondering if there is a solid tutorial or a way to loop through these comboboxes to make this code quick and simple.
Something like this should get you started. It defines a list of combo boxes which you want to check and then determines which ones have a SelectedIndex <> -1. If it doesn't then the value is recorded.
' Build a list of values based on combo boxes with a selected index.
Dim values As New List(Of String)
' Build an array of combo boxes we want to process.
For Each cb As ComboBox In New ComboBox() {comboBox1, comboBox2, comboBox3}
' Check if the current combo box has an index selected.
If cb.SelectedIndex <> -1 Then
' It does - record its value.
values.Add(cb.Text)
End If
Next
' Do something with the values.
MessageBox.Show(String.Join(", ", values.ToArray))
' For example, build a where clause.
' If you do this, be sure to sanitize the values.
MessageBox.Show("WHERE 0=1 " & String.Join(" OR Field=", values.ToArray))

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.

Populate multiple comboboxes with the same values

I have 8 comboboxes on my form that will all hold the same values - Yes and No.
Is there a quicker way than having to do combox1.items.add("Yes") etc?
I have the following but I cant seem to find anything to do with adding the items.
Dim cmb As Control
For Each cmb In Panel1.Controls
If TypeOf cmb Is ComboBox Then
'cmb. isnt beinging anything up for adding items?
End If
Next
Cheers
You can use Enumerable.OfType:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.Items.Add("Yes")
Next
I would create a DataSource containing {Yes,No} values, for example as a List and then just do this:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.DataSource = myYesNoDataSource
Next
Later if you need to accept Y and N in place of Yes and No, you can convert to Dictionary and set ValueMember and DisplayMember accordingly. Plus your list of available values is only initialized once. So your solution becomes flexible.
This Code will be useful, if you didnt use panel.
If you have 5 comboboxes give count 1 to 5 and name of those comboboxes like ComboBox1, ComboBox2, comboBox3 etc.
For count = 1 To 5
Dim combobox = DirectCast(Me.Controls("ComboBox" & n & ""), ComboBox)
combobox.Items.Add("Ok")
Next
Hope this code also helps you.