I'm writing a process to change the back color of every control when the new data is entered. My code works no problem when the control is empty. But doesn't work when I start to enter value or select value from combobox. Besides, the process doesn't work for DateTimePicker.
Can any guru help me? Thank you so much!
Public Sub Control_Change()
Dim ctrl As Control
Dim ans As Long
For Each ctrl In Me.Controls
If ctrl.Name <> "cboProjectHealth" Then
Select Case True
Case TypeOf ctrl Is MSForms.CheckBox, TypeOf ctrl Is MSForms.OptionButton
Select Case ctrl.Value
Case True
ctrl.BackColor = vbWhite
Case Else
ctrl.BackColor = vbBlue: ans = ans + 1
End Select
Case TypeOf ctrl Is MSForms.TextBox, TypeOf ctrl Is MSForms.ComboBox
Select Case ctrl.Value
Case vbNullString
ctrl.BackColor = vbBlue: ans = ans + 1
Case Else
ctrl.BackColor = vbWhite
End Select
Case TypeOf ctrl Is DTPicker
Select Case ctrl.Value
Case vbNullString
ctrl.CalendarBackColor = vbBlue: ans = ans + 1
Case Else
ctrl.CalendarBackColor = vbWhite
End Select
End Select
End If
Next ctrl
End Sub
Related
I have code that works that is
With ClientForm
If retainerFee <> .RetainerFeeTB.Value Then
LabelLB.AddItem (.RetainerFeeLabel.Caption)
BeforeLB.AddItem (retainerFee) 'variable Value
AfterLB.AddItem (.RetainerFeeTB) 'Textbox.value
End If
If less250 <> .Less250TB.Value Then
LabelLB.AddItem (.less250label.caption)
BeforeLB.AddItem (less250) 'variable Value
AfterLB.AddItem (.Less250TB) 'Textbox.value
End If
end with
but I'd like to make it so I don't need to writer the code for every variable that needs to be called
and this is what I have so far for that
With ClientForm
For Each ctrl In .ClientMP.Pages(page).Controls
If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
variableName = Left(ctrl.Name, Len(ctrl.Name) - 2)
variableValue = ??variableName??
tBValue = ctrl.Value
If tBValue <> variableValue Then
labelName = variableName & "Label"
labelCaption = .Controls(labelName).Caption
LabelLB.AddItem (labelCaption)
BeforeLB.AddItem (variableValue)
AfterLB.AddItem (tBValue)
End If
End If
Next
End with
I just don't know how to get the Variable Value
I have been searching for a method to group multiple objects to change a common value but have not been successful. I have been forced to do things like this:
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
RectangleShape8.Visible = True
RectangleShape9.Visible = True
RectangleShape10.Visible = True
RectangleShape11.Visible = True
Ect, Is there a method to group, or declare multiple objects to refer to all of them at the same time? I have attempted to declare but i was unsuccessful. Thanks for your help in advanced.
You can use the following:
Dim RectangleShapeGroup() As String = {"RectangleShape8", "RectangleShape9", "RectangleShape10", "RectangleShape11"}
Dim LabelGroup() As String = {"Label10", "Label11", "Label12", "Label13", "Label14"}
For Each ctrl As Control In Me.Controls
If Array.IndexOf(RectangleShapeGroup, ctrl.Name) > -1 Or Array.IndexOf(LabelGroup, ctrl.Name) > -1 Then
ctrl.Visible = True
End If
Next
In case you want to show all Label controls you can use the following:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
ctrl.Visible = True
End If
Next
... or all controls with names starting with Label or RectangleShape:
For Each ctrl As Control In Me.Controls
If ctrl.Name.StartsWith("Label") Or ctrl.Name.StartsWith("RectangleShape") Then
ctrl.Visible = True
End If
Next
Just add your controls to List(Of ControlType) then loop through to change any properties. This code is for Windows Forms. If this is a different type of application plead indicate that in your question.
Private lstLabels As New List(Of Label) From {Label10, Label11, Label12, Label13, Label14}
Private Sub MakeLabelsVisible()
For Each l In lstLabels
l.Visible = True
Next
End Sub
I have a userform with dynamic checkboxes(programmed)
Below is my code
Dim Rows As Integer
Dim toppart As Integer
Dim Opt As Variant
Dim x As Integer
On Error Resume Next
toppart = 20
UpdateRow = Application.WorksheetFunction.CountA(ActiveSheet.Range("C3:CU3"))
For x = 3 To UpdateRow
Set Opt = Te.Controls.Add("Forms.CheckBox.1", "CheckBox" & x, True)
Opt.Caption = ActiveSheet.Cells(x, "C").Value
Opt.Width = 70
Opt.Height = 18
Opt.Left = 18
Opt.Top = toppart
toppart = toppart + 20
Next
I know if the checkboxes were set via the controls my code will look something like this:
If (CheckBox1.Value = False) Or (CheckBox2.Value = False) Then
MsgBox "You must select alteast 2 checkboxes", vbCritical
But when the checkboxes are dynamically created I can't think of a efficient way to do it. Please any suggestions or Help is very much appreciated, thanks.
Untested, but you can probably loop the controls on your form, check if each is a checkbox, if it is, query it's .Value and tally the total number of checkboxes which are "checked. If that number is LTE 1, then you raise your warning/MsgBox:
Dim checked as Long
Dim ctrl as Object
For Each ctrl in Me.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
checked = checked + 1
End If
End If
Next
If checked <= 1 Then
MsgBox "You must select alteast 2 checkboxes", vbCritical
Exit Sub
End If
I'm using the below code to try and loop through all ListBox controls in a panel on a WinForm. I want to check if any of them have a SelectedIndex above 0. If they do, I want to set a boolean value in an array to True, else set it to False:
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
i = i + 1
If cntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
The issue I am having is that cntrl.selectedindex is not valid as .selectedindex is being picked up that it is not a member of Windows.Forms.Control
How do I get this to see it as a ListBox?
Try converting the cntrl to listbox first like this
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
Dim TmpCntrl As ListBox = TryCast(cntrl, ListBox)
i = i + 1
If TmpCntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
I have UserForm4 which contains CheckBox1...19 and also OptionButton1...3, along with TextBox1, CommandButton1, 2.
When OptionButton 1 = True I want to loop through all CheckBoxes and set each to True.
The error I get states "Cannot find object" and i = 21, n = 23. How are they getting that high, when I only have 19 CheckBoxes?
Thanks!
Private Sub OptionButton1_Click()
Dim ctrl As Control
Dim i As Integer
Dim n As Integer
n = 0
For Each ctrl In UserForm4.Controls
If TypeOf ctrl Is MSForms.CheckBox Then
n = n + 1
End If
Next ctrl
For i = 1 To n
If UserForm4.Controls("CheckBox" & i) = False Then
UserForm4.Controls("CheckBox" & i) = True
End If
Next i
End Sub
Did you create more than 19 initially and delete some? Each VBA object has a unique name. Doing this the way you are doing it is likely to cause all sorts of problems.
For example, if you create 10 CheckBoxes and delete 8 of them, the remaining two might be named Checkbox8 and Checkbox9. So your loop will not hit them at all.
Also, why not do something like the following:
Private Sub OptionButton1_Click()
Dim ctrl As Control
Dim i As Integer
Dim n As Integer
n = 0
For Each ctrl In UserForm4.Controls
'this will make your problem really obvious
debug.print ctrl.name
If TypeOf ctrl Is MSForms.CheckBox Then
ctrl.value = True
End If
Next ctrl
End Sub