Multiple UserForm instances

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Multiple UserForm instances

Post by agibsonsw »

In VBA (Word for my current example) we can create more than one instance of the same form:

Code: Select all

Private frmNewInstance As Object

Private Sub CommandButton1_Click()
    Set frmNewInstance = UserForms.Add(Me.Name)
    frmNewInstance.Tag = "OtherOne"
    frmNewInstance.Show
    
End Sub

Private Sub UserForm_Terminate()
    Set frmNewInstance = Nothing
End Sub
I am anticipating that it will be tricky to distinguish between the different instances, which is why I'm considering using the Tag property.

Are there other pitfalls I should be aware of?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple UserForm instances

Post by HansV »

Do you have a specific reason for wanting to create multiple instances of a userform, or is it just curiosity?
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Multiple UserForm instances

Post by agibsonsw »

Curiosity
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple UserForm instances

Post by HansV »

I wouldn't worry too much then... :grin:

But seriously, a userform is / acts as a class. So just handle it the way you would handle a class.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Multiple UserForm instances

Post by agibsonsw »

Ta @Hans. Yeah, I won't worry :grin:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.