This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding items in a Listbox with multiple columns
With MFC VC++ there are two controls, ListBox and ListCtrl. But with VBA it seems we have only ListBox.
I want to create a listbox with 2 columns (Company_ID, Company_Name).
Here is what I tried:
I created lstbox(control type ListBox)
Row source type = value list
I am taking value from user from two edit boxes and when user clicks "add" then it should be added to the listbox with 2 columns.
In the VBA code routine I added the following lines:
lstbox.ColumnCount = 2
lstbox.AddItem (Company_ID)
The following code is not working which seems to be related with adding column value:
lstbox.Column(1,lstbox.ListCount - 1) = Company_name
This gives error:
Runtime error '424' object required.
Could anyone help with vba code to add to multi column listbox.
Simplified example (with counter):
With Me.lstbox
.ColumnCount = 2
.ColumnWidths = "60;60"
.AddItem
.List(i, 0) = Company_ID
.List(i, 1) = Company_name
i = i + 1
end with
Make sure to start the counter with 0, not 1 to fill up a listbox.
Related
So here is my situation. I have a DataGridView, which has two columns that I am trying to set up as DataGridViewComboBoxColumns called "Received" and "Backordered".
The point of these combo boxes is to create a drop down menu to select how many items are received/backordered when my company receives a shipment. This program will mainly be run on a touch screen setup with no mouse or keyboard, which is why I am opting to use Combo Boxes as opposed to just asking for general user input.
I am attempting to set up the DataGridView as follows:
'Setup of Combo Box Columns
'shipmentData is the name of the DataGridView
Dim receiveCol As New DataGridViewComboBoxColumn()
receiveCol.HeaderText = "Received"
receiveCol.Name = "Received"
shipmentData.Columns.Add(receiveCol)
Dim backorderCol As New DataGridViewComboBoxColumn()
backorderCol.HeaderText = "Backordered"
backorderCol.Name = "Backordered"
shipmentData.Columns.Add(backorderCol)
The above code is in the New() Sub for when the form is created. I am trying to load the data into the ComboBoxes as follows:
Dim rowNum As Integer = 0
For Each op As OrderPart In OrderData.GetPartList()
If op.AmountOrdered > 0 Then
shipmentData.Rows.Add()
shipmentData.Rows(rowNum).Cells("PartNumber").Value = op.PartNumber
shipmentData.Rows(rowNum).Cells("Description").Value = op.Description
shipmentData.Rows(rowNUm).Cells("Ordered").Value = op.AmountOrdered
For it As Integer = 0 To op.AmountOrdered
CType(shipmentData.Rows(rowNum).Cells("Received"), DataGridViewComboBoxCell).Items.Add(it)
CType(shipmentData.Rows(rowNum).Cells("Backordered"), DataGridViewComboBoxCell).Items.Add(it)
Next
rowNum = rowNum + 1
End If
Next
Now when I run the code, the ComboBoxes are created, and their data is added. However whenever I select a data value from the combo box list, and then try to move to another sell I get the following error:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
Why am I getting this error, and how do I fix it? I can't seem to figure out what it is I am doing wrong in my code.
Although I don't know why are adding incrementing numbers to your dropbox, but if you intend to do that, change your code to the following:
For it As Integer = 0 To op.AmountOrdered
CType(shipmentData.Rows(rowNum).Cells("Received"), DataGridViewComboBoxCell).Items.Add(it.ToString())
CType(shipmentData.Rows(rowNum).Cells("Backordered"), DataGridViewComboBoxCell).Items.Add(it.ToString())
Next
I have a listbox in a userform. I use a SQL query to pull data and then populate the listbox. When I look at the query in SQL, it is fine and shows text in those fields. The listbox is showing these numbers though, 4 and 5, what I am assuming is the column name or fields value?
It should say "Landfill/Wellfield" and "O2 levels", so it is not a text length issue. This is all test data. The last column correctly shows "Sparkplugs/Ignition System"
The listbox has 7 columns of data.
Code for the listbox population:
lbx_SSM.Clear
Do Until XL_RSet.EOF = True
For i = 1 To 7
If i = 1 Then
lbx_SSM.AddItem (XL_RSet.Fields(columnName(i)))
Else
If Not (XL_RSet.Fields(columnName(i)) = "Null")
Then lbx_SSM.List(lbx_SSM.ListCount - 1, i - 1) =(XL_RSet.Fields(columnName(i)))
End If
Next i
XL_RSet.MoveNext
Loop
I figured it out, I am not sure what the issue was, but using this to populate the listbox instead of the above does not cause the issue. I've used that first code all the time before so it might be a Fields() issue or something. See below:
XL_RSet.Open
XL_RSet.MoveFirst
i = 0
With Me.lbx_SSM
.Clear
Do
.AddItem
.List(i, 0) = XL_RSet![ID]
.List(i, 1) = XL_RSet![Unit]
.List(i, 2) = XL_RSet![Begin Time] ***Etc for other columns
i = i + 1
XL_RSet.MoveNext
Loop Until XL_RSet.EOF
End With
I've got a form that contains a combobox which contains the values
With io6
.AddItem "90°"
.AddItem "180°"
.AddItem "270°"
.AddItem "360°"
End With
The member of this method I am actually using is .ListIndex. When a user selects 270 degrees, I am sending a 2 to a variable. When I read the variable, I want have the combobox correctly show the appropriate value. In other words, if the variable is 1, I want the combo box to show 180 degrees. Is that achievable without a select\if statement that writes into .Value?
Sub varChange(val as integer)
With comboBox
Select Case val
Case 1
.value = "90°"
Case 2
.value = "180°"
Case 3
.value = "270°"
Case 4
.value = "360°"
End Select
End With
End Sub
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
I'm not sure if I got it right, but I think you want to get the list index of the selected item.
You can get that by
x = io6.ListIndex + 1
The +1 is there since ListIndex is 0 based, but you wanted it 1 based.
And something completely different,
Remove the brackets. Method calls without return values does not use them in VBA
Edit after comment
To take this the other way, i.e getting a value from an index value, do like this:
y = io6.List(x - 1)
sValue = combobox.List(combobox.ListIndex,0)
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
Yes,
I used the following line:
io6.ListIndex = val - 1
So in your code it's:
Sub varChange(val as integer)
comboBox.ListIndex = val - 1
End Sub
I have a listbox which is populated by me adding equipment to it, the listbox is allowed to have duplication when it comes to equipment as I may need 2 pieces of the same equipment to complete the job. when the listbox has been filled with the desired equipment I loop through the contents and put them into their cells, however if there is two pieces of the same equipment I want to only list it once but have a number in the cell to the left of it to represent how many of that particular equipment I need.
ActiveSheet.Range("E" & Rows.Count).End(xlUp).Offset(, 1).Value = "Number of pieces of equipment"
So I need a hand to loop through the listbox to check for duplication and list out the equipment plus the numbers associated with the equipment.
I would not use VBA. I would use a pivot table to count occurrences, then use =getpivotdata to populate the counts in the desired location.
After a few months of coding I realised that this question had many possible answers! What I decided to do was search the listbox for the element that was going to be added and if that item was already in the listbox the user would get a message telling them of there duplication and the element was not added. If the element wasn't present in the listbox it was added to it and the code below shows how I done this!
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
myValue = ListBox1.List(i)
End If
Next i
If ListBox2.ListCount = 0 Then
Me.ListBox2.AddItem myValue
Exit Sub
End If
For i = 0 To ListBox2.ListCount - 1
If ListBox2.List(i) = myValue Then
MsgBox "Hardware already selected"
Exit Sub
Else
Me.ListBox2.AddItem myValue
Exit Sub
End If
Next i
Dim Comp = From C In db.Table1 _
Select C.Completed, C.Taken, C.Namne
Datagridview1.DataSource = Comp
Am using the Entity Framework and Columns Completed and Taken are of bit Datatype. When the query results are displayed in the datagridview, these bit columns are returned as of ColumnType Textbox - so i get a Datagridview textbox column with true or false string values.
I want to display Completed and Taken as Checkbox columns (either ticked for True or un-ticked for false) but ofcourse i can't do this in EditColumn dialogue because the Datagridview is unbound.
how can i change this in code at runtime
Edit:
I just looked at your code again and I realised that what you actually mean is that the DGV is bound rather than unbound? Since you're binding it to Comp.
If I understand correctly, just create 3 columns in the DGV at design time, 1 DataGridViewTextBoxColumn and 2 DataGridViewCheckBoxColumn. Then set the DataPropertyName of each column (Completed or Taken for the checkbox columns etc).
If the DataPropertyNames are correct the data will appear in them (if they're not correct, you'll end up with 6 columns in total).