Same Change event to all comboboxes - vba

I am a VBA noob, so all help is welcome.
I have several Comboboxs in the same UserForm. The Items in every Combobox are loaded in Userform_Inialize. The color of the Combobox will change depending on the item you choose.
Is there a way to apply the same Change event to all Comboboxs or a "dim" of some sort? To avoid coding 200 Ifs.

Write a sub which takes a combobox as a parameter argument - put all the logic there. Then call that sub from each of your combobox event handlers, passing in the specific combobox.
Like this:
Private sub ComboBox1_change()
CheckValue ComboBox1
End Sub
Private sub ComboBox2_change()
CheckValue ComboBox2
End Sub
'check value of `cbo` and assign backcolor as appropriate
Sub CheckValue(cbo As Object)
Select Case cbo.Text
Case "a": cbo.Backcolor=COLORCODE1
Case "b": cbo.Backcolor=COLORCODE2
End Select
End Sub

Related

How to declare an object as a parameter to update a userform?

I have a userform which contain checkboxes and labels.
My goal is to enable or disable specific labels if checkboxes are true or false.
I have:
a module in which I store my functions and subs
a main module
the userform
I could write in the userform:
Private Sub Checkbox1 ()
If Userform.Checbox1 = true then
Userform.label.enable = true
End if
However I have a few checkboxes and labels and I'd like to create a sub or function to simplify my code.
In the module in which I store my function I wrote:
Sub EnableMyLabels(Checkbox as object , Label as object)
If Userform.Checkbox = true then
Userform.label.enable = true
End If
and in my userform I tried to use it like this:
Call EnableMyLabels (Checkbox1 , Label1)
Your Sub should be like
Sub EnableMyLabels(cb As MSForms.CheckBox, lbl As MSForms.Label)
If cb.Value = True Then
lbl.Enabled = True
End If
End Sub
And the call
EnableMyLabels Checkbox1, Label1
You might want to call it from an Event on the Checkbox, eg
Private Sub CheckBox1_Change()
EnableMyLabels CheckBox1, Label1
End Sub
You should try the same principle but using Events.
On your VBE, at the Userform module select "Checkbox1" on the top dropdown.
Then select the method "Click" on the right top dropdown.
A method called Checkbox1_Click should have been created.
That code will run whenever the user clicks the checkbox.
Include all your "label logic" (or logic for other controls too) there.
When you are directly passing the objects (passed byRef by default), then there is no need of the Userform word in your code. Simply drop that and your code should work perfectly.
Sub EnableMyLabels(Checkbox as object , Label as object)
If Checkbox = true then
label.Enabled = true 'fyi - Enabled not Enable
End If
End if

Access VBA put a form element name as an argument

Let's see if anyone knows how to solve this problem:
I have a form with several elements: Some of them are textboxes called A1, A2, A3, A4...
Now, their AfterUpdate SubProcedure is extremely long but barely similar for each of them: A1_AfterUpdate, A2_AfterUpdate, A3_AfterUpdate...etc... are very similar but for the names of the textboxes they change.
My idea was to gather all that was equal in a subprocedure defined this way:
Private Sub Update(Box As String, Menu As Boolean)
If Menu=True{
Me!Box.Text = "This is the text that is going to change"
}
End Sub
So, the only thing I must do is to call it this way, for instance:
Update(A1, True)
But it doesn't seems to work. Any idea on how to reach this objective?
Add a class module - I've called it clsTextBoxEvents.
Add this code to the class:
Public WithEvents txt As Access.TextBox
Private Sub txt_AfterUpdate()
MsgBox txt.Name & " has been updated."
End Sub
In your form module add this code:
Public MyTextBoxes As New Collection
Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Dim txtBoxEvent As clsTextBoxEvents
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
Set txtBoxEvent = New clsTextBoxEvents
Set txtBoxEvent.txt = ctl
txtBoxEvent.txt.AfterUpdate = "[Event Procedure]"
MyTextBoxes.Add txtBoxEvent
End If
Next ctl
End Sub
The MyTextBoxes declaration must be at the very top of the module.
This just adds the AfterUpdate event to all textboxes on the form. You'll probably want to refine that a bit to textboxes with specific text in the name, or controls that are in a specific frame on the form.
If you use a function instead of a sub:
Private Function UpdateCtl(Menu As Boolean)
If Menu Then
activecontrol = "This is the text that is going to change"
End If
End Sub
then you can call it directly from the control's AfterUpdate property: =UpdateCtl(True).
Simple and fast

How to dynamically edit a excel vba form label and tab label?

I have an excel vba form which has some tabs(pages).What I need is, if I double click on label, it should be editable. same is the case for the tabs.
I tried to add some things into the double click function, but not showing any change.
Labels are not editable by the end user
you may adopt this workaround
'change "Label1" occurrences to your actual label name
Private Sub Label1_DblClick()
Me.Label1.Caption = Application.InputBox("enter label text", "label editing", "")
End Sub
You can have a TextBox and disable it. The side effect is you will have textbox edit cursor over the label.
Have two subroutines, one called Labelize and another Textboxize. The former locks the textbox, makes it tranparent, and sets the 3D effect to flat. The latter unlocks it, makes it opaque, and sets the 3D to sunken (the default).
' Makes a textbox look like a label
Private Sub Labelize(txtbox As MSForms.TextBox)
txtbox.Locked = True
txtbox.BackStyle = fmBackStyleTransparent
txtbox.SpecialEffect = fmSpecialEffectFlat
End Sub
' Makes a textbox look like a textbox
Private Sub Textboxize(txtbox As MSForms.TextBox)
txtbox.Locked = False
txtbox.BackStyle = fmBackStyleOpaque
txtbox.SpecialEffect = fmSpecialEffectSunken
End Sub
In the form initialize event set call the Labelize method passing in your textboxes.
Private Sub UserForm_Initialize()
Labelize Me.txtExample
End Sub
In the double-click event per textbox, call the Textboxize method passing in your textbox again.
Private Sub txtExample_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Textboxize Me.txtExample
End Sub
Then in each KeyDown event, check if the key pressed was enter then call the Labelize method.
Private Sub txtExample_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then Labelize Me.txtExample
End Sub

Jump to an item in Listbox

One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?
I already tried the following code
Listbox.ListIndex = index
But it drives me to the error You've used the ListIndex property incorrectly
One property of my list that might be important to mention.
Row source type : Table/Query
Thank you in advance.
Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.
Create a standard module with the code
Sub Main()
UserForm1.Show
Unload UserForm1
End Sub
Insert a userform and visually do something like
Go into the userform code and add
Private Sub CommandButton1_Click()
Dim v As Long
For v = 0 To ListBox1.ListCount - 1
If TextBox1 = ListBox1.List(v) Then
ListBox1.Selected(v) = True
End If
Next v
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("text1")
.AddItem ("text2")
.AddItem ("text3")
End With
End Sub
Run Main Macro
Type in the box : text2
The text2 will be selected in the list

populate values in to two textboxes from a single userform

I am writing a code in Ms-Word vba.
I made two userform say userform_1 and userform_2
userform_1 contains two buttons and two textboxes.
userform_2 is a calendar userform.
I wrote a code for showing a calendar useform on each button click event.
Now, I want to write a code such that when user clicks on 1st button the selected calendar values will display in textbox1 and when user clicks on 2nd button the selected value displayed in the 2nd textbox. But it displays the same value in textboxes.
Please let me know this how could i achieve this...
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Main.Event_DblClick = True
Dim SampleDate As Date
frmAE_Tool.TextBox1.Text = Me.Value
frmAE_Tool.TextBox2.Text = Me.Value
End Sub
What you can do is to create a Sub in your userform_2 which going to determine the output of the calendar.
Userform_2 :
Declare a Private variable like below :
Private mTextBox As Control
(This textbox would be the output textbox of your calendar)
Add the following Sub :
Public Sub SetOutput(pTextBox As Control)
Set mTextBox = pTextBox
End Sub
(This Sub would allow to decide which output you want to use)
Modify your CmB_DblClick Sub like this :
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Others things that I didn't understand
mToolbox = Me.Value
End Sub
Userform_1 :
For the first button, modify the Click Sub like this :
Private Sub Btn1_Click
[...]
YourCalendarForm.SetOutput Textbox1
End Sub
And for the second, like this :
Private Sub Btn2_Click
[...]
YourCalendarForm.SetOutput Textbox2
End Sub