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
Related
I am trying to apply if condition on data bound combobox but my code is not working. my combobox are getting data ComboBox5.DataSource = table2,ComboBox6.DataSource = table2,ComboBox6.DisplayMember = "name" from sql, My combobox has multiple names like "bob","sam","john" etc. I want to hide another combobox when i select "bob" else that comboboxes should visible. how should i do it?
Dim rv As Object = ComboBox3.Items.Cast(Of Object)().Where(Function(r) ComboBox3.GetItemText(r) = "bob").FirstOrDefault()
If ComboBox3.SelectedText = rv Then
ComboBox5.Visible = False
ComboBox6.Visible = False
ElseIf ComboBox3.SelectedText <> rv Then
ComboBox5.Visible = True
ComboBox6.Visible = True
End If
Here is some code that checks if a TextBox.Text value is in a Combobox.
Dim fn As String
For Each fn In cbType.Items
If fn = tbType.Text Then
Do the = Code from your project
Return
End If
Next
Dim fn2 As String
For Each fn2 In cbType.Items
If fn2 <> tbType.Text Then
Do the <> Code from your project
Return
End If
Next
Struggling with some code here, looking for help. I am trying to figure out the correct syntax for updating a textbox value based on whether another textbox has a value or not.
Breakdown
Textbox1 name = arisk_box
Textbox2 name = adjust_box3
Textbox3 name = rr_box
This is what I have so far:
Private Sub arisk_box_Change()
If arisk_box.Text <> "" And adjust_box3.Text = "" Then
rr_box.Value = arisk_box.Value
ElseIf arisk_box.Text <> "" And adjust_box3.Text <> "" Then
rr_box.Value = adjust_box3.Value
End If
End Sub
*If arisk_box has a *VALUE* and adjust_box3 *DOES NOT* then rr_box = the value from arisk_box
Elseif arisk_box AND adjust_box3 both have a *VALUE* then rr_box = the value from adjust_box3
The simplest way it to use 2 separate macros, one for each textbox change event. But it may not work as smoothly as you want it to. I sure someone will have a more advance method.
Private Sub adjust_box3_Change()
If arisk_box <> "" And adjust_box3 = "" Then rr_box = arisk_box
End Sub
Private Sub arisk_box_Change()
If arisk_box <> "" And adjust_box3 <> "" Then rr_box = adjust_box3
End Sub
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
I'm rewriting some code and had a thought, but can't seem to get my syntax right to execute it properly. I want to use a for loop to populate an array of commandbuttons as well as control their visibility. I just need help with my syntax to define which CommandButton number I'm working on in the loop. For instance, CommandButton1, CommandButton2, etc.
Public Sub LoadLots(sName As String, streamLots() As String)
Label1.Caption = sName
For o = 1 To 9
If streamLots(o) <> "" Then
CommandButton& o &.Caption = streamLots(o)
CommandButton& o & .Visable = True
Else
CommandButton& o & .Visable = False
End If
Next
End Sub
Use the Userform.Controls collection to reference the commandbuttons by name.
Public Sub LoadLots(sName As String, streamLots() As String)
Dim btn As MSForms.CommandButton
Label1.Caption = sName
For o = 1 To 9
Set btn = Me.Controls("CommandButton" & o)
If streamLots(o) <> "" Then
btn.Caption = streamLots(o)
btn.Visible = True
Else
btn.Visible = False
End If
Next
End Sub
I have in my Excel userform several checkboxes (in total 10) which all should be have the same behavior. It means, when I reload the form the value of the checkbox should change, depending on values from the spreadsheet with the actual selected row.
The following code is working properly for one checkbox:
If Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "X" Then
cbERW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "?" Then
cbERW.Value = Null
Else
cbERW.Value = False
End If
How can I do this in the easiest way for multiple checkboxes? But for every checkbox the referring row and column could change. For example, the next one look like this (cbHSAW instead of cbERW, Column B22 instead of B21):
If Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "X" Then
cbHSAW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "?" Then
cbHSAW.Value = Null
Else
cbHSAW.Value = False
End If
Anybody know how to do this easily?
You can wrap your test statement in a function and pass the range for the specific checkbox. Then set the return result can be as the value for the checkbox. The below needs to be modified for your form and ranges but will give the desired result.
'Function to test the value of the range
Private Function SetCheckBox(ByVal rng As Range)
If rng.Value = "X" Then
SetCheckBox = True
ElseIf rng.Value = "?" Then
SetCheckBox = Null
Else
SetCheckBox = False
End If
End Function
Private Sub UserForm_Initialize()
'Set value for each checkbox by passing its range to the function
CheckBox1.Value = SetCheckBox(Sheets("aux").Range("A1"))
CheckBox2.Value = SetCheckBox(Sheets("aux").Range("A2"))
CheckBox3.Value = SetCheckBox(Sheets("aux").Range("A3"))
CheckBox4.Value = SetCheckBox(Sheets("aux").Range("A4"))
End Sub