Emlyn Pagden
Emlyn Pagden

Reputation: 33

How to add pages to a MultiPage at runtime successfully, without getting run-time errors or object disconnection errors

I'm trying to create a userform which starts with two multipages, one behind the other, which will contain separate type of data, say, one is accounts and the other is marketing. The idea being, if the user clicks the marketing button on the user form, then they get multipage1 and if they click on accounts, they get multipage2.

The issue I'm having is that, if I create the MPs programatically and then later call a funciton to create more pages then I receive the "Object has disconnected from the Clients" error. If I use the approach in this post, i.e. create the MPs to begin with, then I get, Run-Time error, Method Add of object Pages failed. This error ocurs when I try and add a page to mp1. So, I decided to write the code below as a test loop, to show what I'm trying to do. It's basically simulating a user adding 10 new pages to their marketing page. Maybe these will be 10 new marketing schemes, who knows. Then the same for the accounts multipage. the point is, the below is giving the same error when I try and add a page in my actual project. So I'm thinking, if I can fix the code below then I can apply the same fix to my project. Any help would be greatly appreciated. I've been trying to fix this for two weeks now.

Private Sub UserForm_Initialize()

page_num = 0

Set mp1 = Me.MultiPage1
Set mp2 = Me.MultiPage2

Do
  With mp1
    .Enabled = False
    .Visible = False
  End With
  With mp2
    .Enabled = True
    .Visible = True
    .SetFocus
    .Pages.Add
    .Pages(page_num).Caption = page_num
  End With
  With mp2
    .Enabled = False
    .Visible = False
  End With
  With mp1
    .Enabled = True
    .Visible = True
    .SetFocus
    .Pages.Add
    .Pages(page_num).Caption = page_num
  End With
  page_num = page_num + 1
      
Loop While page_num <> 10
      
End Sub`

Upvotes: 0

Views: 129

Answers (1)

Emlyn Pagden
Emlyn Pagden

Reputation: 33

It appears the easiest way to resolve this problem is to put the multipages inside a Frame! Who knew.

Dynamically adding a page to a multipage more than one multipage on the form

Upvotes: 2

Related Questions