1

Here is my problem:

I have an userform with a MultiPage. On the first page I have a TextBox and a CommandButton. The idea here is when I enter a number in my TextBox, and I click the CommandButton, I create a number of new TextBox equal to the number entered in the first TextBox. Additionally, it create also a new CommandButton.

What I want to do now is to assign a macro to this new CommandButton created dynamically.

I use this post to help me fix my problem and it works if I want to print a message as shown in the post.

In my case, what I want to do by clicking the new CommandButton, is to create new pages in my MultiPage using the name entered in the TextBoxes created by the first CommandButton.

I have this class (it's called Classe3):

Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton

Private Sub CmdEvents_Click()

Dim multi_page As Object

Dim nb_blocks As Integer  'Represents the number entered in the first text box on the user form
Dim i As Integer          

nb_blocks = Me.MultiPage1.Pages(0).UF4_TB_nbC.Value 'UF4_TB_nbC is the name of th first TextBox

For i = 1 To nb_blocks

    Set multi_page = Me.MultiPage1.Pages.Add("page" + CStr(i), i)

    With multi_page

        .Caption = Me.MultiPage1.Pages(0).Controls("UF4_TB_nameBlock" + CStr(i)).Value 'Get the name from each TextBox created dynamically

    End With

Next i

End Sub

In my UserForm I have this part of the code in my Sub CommandButton1_Click():

Set command_button = Me.MultiPage1.Pages(0).Add("Forms.CommandButton.1")

With command_button

    .Height = 18
    .Top = 96 + 18 * nb_blocks + 18
    .Width = 60
    .Left = 44
    .Caption = "Ok"
    .name = "UF4_CB2_Validation"
    .Tag = 1

End With

ReDim Preserve cmdArray(1 To 1)
Set cmdArray(1).CmdEvents = command_button 'Assign the macro the the new CommandButton

Set command_button = Nothing 

So when I run my UserForm and I press the second CommandButton to build my pages I have an error message from my class. More specifically from this line:

nb_blocks = Me.MultiPage1.Pages(0).UF4_TB_nbC.Value

The error message says that MultiPage1 can't be found.

Maybe the problem comes from the argument Me but I'm not sure. If you have any idea it will be a pleasure to discute about it to fix my problem.

braX
  • 11,506
  • 5
  • 20
  • 33
  • So I fixed the problem. Actually there is no _link_ between the class and the forms from my user form. What I did is to add `Public frm As Object` in my class and `Set cmdArray(1).frm = Me` in my user form code. I guess that now we can use the data from the user form in the class module. – Nikola Lakic Apr 09 '20 at 09:42

0 Answers0