Setting same SelectedIndex value for multiple combobox - vb.net

I have a form where i have 23 Comboboxes. Writing SelectedIndex=-1 for every combobox will definitely work but i want to know whether there's any other way of doing it as done in below given example. ?
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next
i tried this,
If TypeOf ctl Is ComboBox Then
ctl.selectedIndex=-1
End If but it doesn't works. Please help me out.

In your example your ctl variable is a Control and not a Combobox so it does not have the SelectIndex property - though you could have casted it back with DirectCast(ctl, Combobox).
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is Combobox Then
DirectCast(ctl, Combobox).SelectedIndex = -1
End If
Next
Or create a loop of type specific control for your loop. No need to check type here.
Dim cbs = GroupBox1.Controls.OfType(Of Combobox)()
For Each cb In cbs
cb.SelectedIndex = -1
Next

Related

Word VB, Clear all checked fields

i have a word with 15+ VB forms and 20-50 CheckBoxes in each.
How do i clear (if they are cheched) all the CheckBoxes in active form w/o having to write the name of each CheckBox?
Thank you
This code will do the job. It must be placed in the UserForm's code sheet.
Private Sub ClearCheckBoxes()
Dim Ctl As MSForms.Control
For Each Ctl In Me.Controls
If TypeName(Ctl) = "CheckBox" Then Ctl.Value = False
Next Ctl
End Sub

VBA - Loop through Controls on a form and read the value

I would like to loop through controls on a form and read the value. However the Value and Checked properties are not available. My question is, how can I read the value of a control (in this case a checkbox) when I loop through them?
Dim Ctrl as Control
For Each Ctrl In frmMaintenance.Controls
If Left(Ctrl.Name, 7) = "chkType" And **Ctrl.Value = True** Then
End if
Next Ctrl
loop through the controls and check the TypeName.
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "CheckBox" Then
MsgBox c.Value
End If
Next
TypeName will work, but at the end of the day it's a string comparison.
The actual syntax for strongly-typed type checks in VBA goes like this:
TypeOf [object] Is [Type]
So:
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.CheckBox Then
Debug.Print TypeName(ctrl), ctrl.Name, ctrl.Value
End If
Next
Now, somewhat awkwardly the MSForms library is making OptionButton and CheckBox implement the same interface (not all that surprising actually), so the above condition will be True for both types of controls; you can filter out the OptionButton with another type check:
If TypeOf ctrl Is MSForms.CheckBox And Not TypeOf ctrl Is MSForms.OptionButton Then
Arguably, using TypeName is simpler, at least in this case where MSForms is being annoying. But you have to know about TypeOf ... Is when you start needing to do type checks in VBA.

Access 2016 VBA - Create custom collection of controls, manually specifying control's name

Since you can't have a vertical Control Tab in Access, I'm creating a fake one.
I wrote this piece of code to highlight (change fore color to white) the current page:
Private Sub updateBtnColor()
Dim currentPageIndexSZero As Long
Dim ctl As Control
currentPageIndexSZero = Me.tab1
For Each ctl In Me.Controls
If ctl.Tag = "page" & CStr(currentPageIndexSZero) Then
ctl.ForeColor = white
ElseIf InStr(1, ctl.Tag, "page", vbBinaryCompare) Then
ctl.ForeColor = black
End If
Next ctl
End Sub
Every button has got the page it's referring to as a tag, in the form of:
page0
page1
...
pageN
So the loop basically checks the current page, finds the button with the appropriate tag (assuming I named them correctly) and highlights its text.
Now this can be slow since I've got a heavy loaded form or bad practise, so I thought of creating a custom collection instead of looping through the whole Controls Collection.
I wanted to create such structure so I can loop through it, something like:
Enum myButtons
button1 = Forms!myForm!button1
button2 = Forms!myForm!button2
button3 = Forms!myForm!button3
button4 = Forms!myForm!button4
End Enum
And then:
Public Sub updateBtnColor()
Dim curPageZ as Long
Dim button as Control
For Each button in myButtons
With button
If Right$(CStr(.Name, 1)) = curPageZ
.ForeColor = 16777215 ' White
Else ' No need for an additional ElseIf since I already know these are only the wanted buttons
.ForeColor = 0 ' Black
End If
End With
Next button
End Sub
What is the most elegant/fastest/best/proper way to create such structure or, if my idea is not that good, to create such logic?
Thanks!
Cycling thru controls works quite fast, but if you want to speedup this, use Collection for storing objects, not Enum. Collection can be cycled also using For Each....
On form module level create a collection for your buttons:
Dim mcolMyButtons As New Collection
Then fill this collection in Open or Load events. You can use tags:
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is CommandButton Then
If ctl.Tag Like "page*" Then
mcolMyButtons.Add ctl
End If
End If
Next
Or just directly add buttons you want:
mcolMyButtons.Add Me.Button1
mcolMyButtons.Add Me.Button2
mcolMyButtons.Add Me.Button3
mcolMyButtons.Add Me.Button4
Then your sub:
Private Sub updateBtnColor()
Dim ctl As Control
For Each ctl In mcolMyButtons
If ctl.Tag = "page" & Me.tab1 Then
ctl.ForeColor = vbWhite
Else
ctl.ForeColor = vbBlack
End If
Next ctl
End Sub

How to change multiple comboboxes with little code

Here's my problem.
I have a userform with 10 comboBoxes.
Instead of Select Case al those comboboxes one by one is
there a smart way to change multiple combobox properties such as backcolor or fontcolor?
So for example:
Sub ChangeMultipleComboBoxes()
Dim comboB as control
For Each comboB In Me.Controls
If TypeName(comboB) = "ComboBox" Then
comboB.BackColor = vbRed
End If
Next comboB
End Sub
The problem is that for comboB I can't seem to find any properties for changing it's backcolor or whatsoever.
Can someone help me please?
As Zaider says, your code works. You just don't get the IntelliSense for the ComboBox-specific properties when it's declared as a Control, even though you can use the properties as you've done in your code. (Note that properties common to all controls, e.g., Caption are available.)
To get the Intellisense for all ComboBox properties, re-declare the Control as a ComboBox once you've determined that's what it is:
Private Sub UserForm_Click()
Dim ctl As MSForms.Control
Dim comboB As MSForms.ComboBox
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
're-declare it as a ComboBox
Set comboB = ctl
'Now you get IntelliSens
comboB.BackColor = vbRed
End If
Next ctl
End Sub

Toggle checkboxes in a tab based upon master checkbox selection

I'm having issues with an onclick event for a checkbox in a vba form. Basically what I am trying to do is ammend the value of all checkboxes on specific tab to be the same value as a master checkbox. In this instance, it is the 'Use Online' captioned checkbox below (online_toggle in the code) which once clicked should toggle the other checkboxes on the tab 'on' or 'off'. I currently have the following code but it keeps producing an error at 'For Each obj In online.OLEObjects'
Private Sub online_toggle_Click()
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
If ctl.GroupName = "online_variants" Then
If ctl.Name <> "online_toggle" Then
ctl.Value = online_toggle.Value
End If
End If
End If
Next ctl
End Sub
N.B. online is the name of the tab on which all of the checkboxes are situated. If it helps the checkboxes affected by the master checkbox are all grouped as online_variants
Cheers,
Jason
In a Mutipage the page numbering starts from 0 so if you are trying to refer to the Checkboxes in the Online tab (7th Tab) then use this
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
'~~> Your code here
Debug.Print ctl.Name
End If
Next