VBA ComboBox Value by Index - vba

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

Related

How to change visibility of a combo box based on another combo box value in VBA

I am using Microsoft Access at the moment.
I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:
Private Sub cmb_BoxValue_AfterUpdate()
Dim Size1 As Integer
Size1 = Me.cmb_BoxValue.Value
Select Case Me.cmb_BoxValue
Case 1
Me.cmb_boxBox1 = True
etc...
But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you
Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop, with a test if index is <= cmb_BoxValue input to set Visible property:
For x = 1 to 20
Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next

If cell is blank then make variable equivalent to any value

I have a workbook with 3 List Boxes. Each List Box is populated by a list that is created, each List Box contains a "Blank" data point. I am trying to write a script that goes:
For lpRows = 1 to 100
For lpColumns = 1 to 8
If ListBox1 = "" and ListBox2 = "Val" And ListBox3 = "Var" Then
Sheets(RndmSheet).Cells(lprows,lpcolumns) = Sheets(DiffSheet).Cells(lprows,lpColumns)
else
end if
However, my code isn't working because VBA is simply searching for a blank cell, where I would prefer it to return any value that contains both ListBox2 and ListBox3, but Listbox1 would be irrelevant.
Is there an easy way to use a wildcard to achieve what I want or will I need to simply make a rather large nested if statements to handle this?
I just added in an if statement
if sheets(rndmsheet).cell(lpRows,lpColumns) = "" then
VarCell = Sheets(rndmsheet).cell(lprows,lpcolumns)
end if
Sorry if that doesn't really make sense.

Gridview compare to list and set value to gridview checkbox

I have a grid view which has 3000 rows and one checkbox column and 2 textbox column.
I have a parameter list around say around 1700. it varies sometimes 5 or 10.
So I need to compare the list to the grid view and set the 1st checkbox column to true.
I use the compare and select method like below code which works well for a small number of parameter like 50,70
If Paramlist.Count > 0 Then
Dim row As Integer = 0
For Each listItem In Paramlist
Do While (row < gridview1.RowCount)
If Trim(gridview1.Rows(row).Cells(1).Value) = Trim(listItem) Then
gridview1.Rows(row).Cells(0).Value = True
Exit Do
End If
row = row + 1
Loop
Next
End If
It takes a lot of time to compare and set gridCheckbox value to true when I do it with more parameter list and the application seems like it is hanging when the user uses it.
Could anyone suggest me a faster way with less time complexity / best practice to set the grid check box value to true in the grid view.
[UPDATE]
I have attached a image of the exception thrown.
System.Invocation.TargetInvocationException

DevExpress XtraGrid GroupRow, CheckEdit Interactions Possibilities

Currently, I am displaying an XtraGrid that contains Group Rows. I have a "Select All" DevExpress.XtraEditors.CheckEdit control (which is different from this elusive "Select All" check box control I am reading about in the documentation). It is different for a reason: I want the check box to do something other than "Select All" (which comes in only three different varieties according to the DevExpress Docs.).
I want a user to be able to do one of two things with the CheckEdit control. [1] If no Group Rows are expanded, I want to select all Group Rows. [2] If one or more Group Rows are expanded, I only want to select the expanded rows.
Presently, I am able to manipulate the controls to do only one of the two things (see code). My question is twofold: is this possible; and, if so, how would I go about it?
Here is my code that does the second of the two 'things' described above:
'If the CheckEdit control is checked:
xtraGrid.SelectAll()
Dim rowHandles() As Int32 = xtraGrid.GetSelectedRows()
If rowHandles.Count > 0 Then
For Each RowHandle As Int32 In rowHandles
If xtraGrid.IsGroupRow(RowHandle) Then
xtraGrid.UnselectRow(RowHandle)
End If
Next
End If
As you can see, all this really is just a work around. There is also presumably more overhead than needed in my calling .GetSelectedRows(). I am just attempting to sort through the row types in order to keep the row selected or .UnselectRow()
You can use GridView.GetRowExpanded method to check whether a specific group row is expanded. For iterating through visible rows you can use GridView.RowCount property. To get row level just use GridView.GetRowLevel method. Also you need to check wether a row is new item row or is filter row by using GridView.IsNewItemRow method and GridView.IsFilterRow method. For all of this methods you need to get Row handle by using GridView.GetVisibleRowHandle method. When you are working with selection is better to use GridView.BeginSelection method and GridView.EndSelection method outside of your code.
UPDATE: If you want to select hidden rows within a collapsed group then you can use GridView.GetChildRowCount method and GridView.GetChildRowHandle method to get all hidden rows.
Here is some sample code that is performing your two things together:
Private Sub SomeSub
If xtraGrid.GroupCount = 0 Then
xtraGrid.SelectAll()
Exit Sub
End If
xtraGrid.BeginSelection()
xtraGrid.ClearSelection()
Dim isExpanded As Boolean = False
For rowVisibleIndex = 0 To xtraGrid.RowCount - 1
Dim rowHandle As Integer = xtraGrid.GetVisibleRowHandle(rowVisibleIndex)
If xtraGrid.IsNewItemRow(rowHandle) Then
Continue For
End If
Dim level As Integer = xtraGrid.GetRowLevel(rowHandle)
If level = 0 Then
If Not isExpanded Then
isExpanded = xtraGrid.GetRowExpanded(rowHandle)
If isExpanded Then
xtraGrid.ClearSelection()
Else
xtraGrid.SelectRow(rowHandle)
End If
End If
Else
xtraGrid.SelectRow(rowHandle)
'Update: select hidden rows
If xtraGrid.IsGroupRow(rowHandle) And Not xtraGrid.GetRowExpanded(rowHandle) Then
SelectRowHierarchy(rowHandle)
End If
End If
Next
xtraGrid.EndSelection()
End Sub
Private Sub SelectRowHierarchy(rowHandle As Integer)
Dim childCount As Integer = xtraGrid.GetChildRowCount(rowHandle)
For childIndex As Integer = 0 To childCount - 1
Dim childRowHandle As Integer = xtraGrid.GetChildRowHandle(rowHandle, childIndex)
xtraGrid.SelectRow(childRowHandle)
If xtraGrid.IsGroupRow(childRowHandle) Then
SelectRowHierarchy(childRowHandle)
End If
Next
End Sub

How to get the current Column Index while editing a cell in DataGridView using VB.NET

I have a DataGridView with 5 columns.
The Cells of Columns 1 and 5 are ComboBoxes.
I have a function that runs whenever I change any of these ComboBoxes.
Now, for the function to run properly, I have to get which Column does the ComboBox that I have edited belongs to.
Its like, when I change the ComboBox that belongs to Column 1, Function 1 runs.
When I change the ComboBox that belongs to Column 5, Function 2 runs.
Or
DataGridView.CurrentCell.ColumnIndex
Then if your have a predefined columns in DataGridView(for example name of column will be DataGridView_ComboBoxOne) and don't want hardcode a compare of indexes
You can use like this:
Select case DataGridView.CurrentCell.ColumnIndex
Case DataGridView_ComboBoxOne.Index
Function1()
Case DataGridView_ComboBoxTwo.Index
Function2()
Case Else
'Update of other columns
End Select
Dim columnIndex as Integer
columnIndex = e.ColumnIndex
This is the straight way to get the column index without using current cell. Use it under DataGridView1_ColumnHeaderMouseClick.
Ah silly me,
DataGridView.CurrentCellAddress.X 'Column
DataGridView.CurrentCellAddress.Y 'Row