PDA

View Full Version : [SOLVED:] Linking Label Caption To Textbox



Logit
01-23-2017, 09:13 PM
I need a macro code that links an array of Labels to an array of Textboxes.

These controls are located on a Userform.

Ex: Label1.Caption = Textbox1.Text ... Label2.Caption = Textbox2.Text.

There will be a number of these controls on the UserForm, so using : Label1.Caption = Textbox1.Text for each and every pair will be unreasonable.

How can an array of Labels and Textboxes be set to handle this ?

Thanks.

pike
01-24-2017, 02:18 AM
Hi,
Possibly with your code
Class module named clsTextBox


Option Explicit
Private WithEvents MyTextBox As MSForms.TextBox

Public Property Set Control(tb As MSForms.TextBox)
Set MyTextBox = tb
End Property

Private Sub MyTextBox_Change()
Call UserForm1.PersistentUpdate_ItemNumber(MyTextBox.Name)
End Sub


and in the userform1 module

Option Explicit
Dim tbCollection As Collection

Private Sub UserForm_Initialize()

Dim ctrl As MSForms.Control
Dim obj As clsTextBox

Set tbCollection = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then

Set obj = New clsTextBox
Set obj.Control = ctrl
tbCollection.Add obj
End If
Next ctrl
Set obj = Nothing

End Sub

Sub PersistentUpdate_ItemNumber(MyName As String)
Dim myIndex As Long
myIndex = Right(MyName, Len(MyName) - 7)
Me("Label" & myIndex).Caption = MyName
End Sub

snb
01-24-2017, 03:10 AM
sub M_snb()
for j=1 to 300
me("Label" & j).caption=me("Textbox" &j)
next
End Sub

Logit
01-24-2017, 07:47 AM
.
PIKE:

Receiving error on
Dim obj As clsTextBox "User Defined Type Not Defined"

snb (http://www.vbaexpress.com/forum/member.php?44644-snb) :

Does your Sub replace one in PIKE's submission or is it an addition ?
Where does it go ... how to call it.

Logit
01-24-2017, 07:57 AM
Pike:

Disregard my previous. Got it working. However, instead of the Label accepting data input into the Textbox as it's caption, it is using the Textbox name as it's caption.

Whatever is entered into the textbox field should also become the associated Label Caption.

??

Paul_Hossler
01-24-2017, 08:26 AM
Guessing, but try this




Sub PersistentUpdate_ItemNumber(MyName As String)
Dim myIndex As Long
myIndex = Right(MyName, Len(MyName) - 7)
Me("Label" & myIndex).Caption = Me("Label" & myIndex).Text
End Sub

Logit
01-24-2017, 08:34 AM
Paul:

You da man !

Made a slight edit to the code :



Sub PersistentUpdate_ItemNumber(MyName As String)
Dim myIndex As Long
myIndex = Right(MyName, Len(MyName) - 7)
Me("Label" & myIndex).Caption = Me("Textbox" & myIndex).Text '<--- From "Label" to "Textbox"
End Sub


Thanks to all for your help !

pike
01-27-2017, 12:43 AM
Hello Logit,
The syntax below is universal with the userform name also captured.
Class Code Named clsFrmCtls

Option Explicit
Public mName
Public nlabel
Public mFrm As Object
Public Event SelectedChange(objCtr, intCtr)
Public WithEvents mTextBox As MSForms.TextBox
Private Sub mTextBox_Change()
nlabel = Replace(mName, "TextBox", "")
RaiseEvent mFrm.SelectedChange(mName, nlabel)
End Sub

Userform Code


Option Explicit
Dim mcolEvents As Collection
Public Sub SelectedChange(objCtr, intCtr)
Me.Controls("Label" & intCtr).Caption = Me.Controls(objCtr).Text
End Sub
Private Sub UserForm_Initialize()
Dim cCBEvents As clsFrmCtls
Dim ctrl As MSForms.Control
Set mcolEvents = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
Set cCBEvents = New clsFrmCtls
Set cCBEvents.mFrm = Me
cCBEvents.mName = Me.Controls(ctrl.Name).Name
Set cCBEvents.mTextBox = Me.Controls(ctrl.Name)
mcolEvents.Add cCBEvents
End If
Next ctrl
End Sub
Private Sub butClose_Click()
Unload Me
End Sub
Private Sub UserForm_Terminate()
Set mcolEvents = Nothing
End Sub

Logit
01-27-2017, 07:22 AM
Thanks friend !

snb
01-27-2017, 10:53 AM
If using a class, the only code you need is:

In the userform:


Dim sn() As New c_check

Private Sub UserForm_Initialize()
ReDim sn(Controls.Count)

For Each it In Controls
If TypeName(it) = "TextBox" Then Set sn(j).cl_Text = it
j = j + 1
Next
End Sub

In the classmodule c_check


Public WithEvents cl_Text As MSForms.TextBox

Private Sub cl_Text_Change()
cl_Text.Parent(Replace(cl_Text.Name, "TextBox", "Label")).Caption = cl_Text.Text
End Sub

pike
01-28-2017, 04:44 PM
good one snb
using Parent to trap the userform

ayush842001
08-05-2020, 05:07 AM
pls infom in Excel VBA, how to change the present caption of checkbox mentioned inside the user form to a new dynamic manual caption when command button is clicked and transfer the updated value in the excel she