vbaformsmultipagemulti-page-application

Rename TextBoxes on Form after Copying it into a new multipage


I'm dealing with this issue. I have a userform that addapts after the user enters a value (for example, if he wants to add 3 members, the user form creates 3 different pages with the same boxes, after renaming them).

I have this right now:

Example

After pressing "Lanzar", the userform will create more pages with "Datos Educando 2", "Datos Educando 3"... and so on, taking the value introduced on "Educandos a inscribir:"

The code is the following:

Private Sub lanzar_numero_educandos_Click()
    
    Dim l As Double, r As Double
    Dim ctl As Control
    
    Me.MultiPage1.Pages(1).Visible = True
    
    If Me.MultiPage1.Pages.Count > 2 Then
    
        For a = Me.MultiPage1.Pages.Count - 1 To 2 Step -1
        
            Me.MultiPage1.Pages.Remove a
        
        Next a
    
    End If
    
    Me.MultiPage1.Pages(1).Visible = True
    
    If educandos_a_inscribir.Value <> 1 Then
    
        For a = 1 To educandos_a_inscribir.Value
        
            MultiPage1.Pages.Add
            MultiPage1.Pages(a).Controls.Copy
            MultiPage1.Pages(a + 1).Paste
            
            For Each ctl In MultiPage1.Pages(a).Controls
                If TypeOf ctl Is MSForms.Frame Then
                    l = ctl.Left
                    r = ctl.Top
                    Exit For
                End If
            Next
        
            For Each ctl In MultiPage1.Pages(a + 1).Controls
                If TypeOf ctl Is MSForms.Frame Then
                    ctl.Left = l
                    ctl.Top = r
                    Exit For
                End If
            Next
            
            Me.MultiPage1.Pages(a + 1).Caption = "Datos Educando " & a
        
        Next a
        
        Me.MultiPage1.Pages(1).Visible = False
        
    End If

End Sub

Now, the problem I have is that the pages generated with this code have random names on each TextBox, so I'm not able to locate the information introduced from the user.

For example, this is the first page (the one that has the names I already know):

Second Example

Here, the TextBox "Nombre Educando" is called "nombre_educando_1", so I can locate it easily on code:

TextBox Name

The Textboxes created when copying the first page, have random names (like "TextBox 34", "TextBox 35"... and so on), so I'm not able to controll how are they called.

There is a way of generate the pages editing the TextBox names? Like, for example, for the second page generated, the TextBox in the example above should be "nombre_educando_2" and so on.

Thanks!


Solution

  • Each control on the first page has an attribute named Tag. You can value these with unique and meaningful values. When you paste the controls to the new page the Tag values will follow. Then as you loop through the controls on any page, use a Select Ctl.tag statement to identify what to do with the value of the control.