Opening a Userform from within Userform

ZilchSr

New Member
Joined
Jan 27, 2015
Messages
4
My directive is to use a button called "Legend" on a userform called "PlatformData" to open another userform called "PlatformLegend". Both userforms are for viewing existing data only, I don't wish to hide the first userform, and as far as I am aware, it doesn't matter if is it modeless or modal because I don't need to perform any actions while either userform is being viewed other than the ability to unload them. My issue is that I am able to get the second userform opened and viewed, however, none of the initialization code is executing (not even a simple MsgBox function), and is showing just the bare userform (all of my controls are dynamically added with VBA code). What am I doing wrong here?


Here is the relevant code for the first userform called "PlatformData" that has the "Legend" button to be used to view the second userform:

Code:
Private Sub CommandButton1_Click()
    ' OK button to unload PlatformData userform
    Unload Me
    Exit Sub
End Sub

Private Sub CommandButton2_Click()
    ' Legend button to show PlatformLegend
    Worksheets("Home").Activate
    PlatformLegend.Show
End Sub

Private Sub Userform_Initialize()
    MsgBox "Blah blah blah blah this is my first userform blah blah blah"
End Sub

Here is the relevant code for the second userform called "PlatformLegend":

Code:
Sub Userform2_Initialize()
    MsgBox "Show me my controls!"
    Dim ccControl As Control
    Set ccControl = Me.Controls.Add("Forms.Label.1", "LegendTest", True)
    With ccControl
        .Caption = "Hello"
        .Top = 42
        .Left = 12
        End With
End Sub

Any feedback would be greatly appreciated, and thank you in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the board...
You can create another button in your "PlatformData" userform with the code:


PlatformLegend.show
 
Upvote 0
Welcome to the board...
You can create another button in your "PlatformData" userform with the code:


PlatformLegend.show

Thank you so much for the quick feedback! The second Sub in my code shown above does just that. The issue is that PlatformLegend userform IS "showing", but with none of its VBA code (shown in my coding above, second section) executing.
 
Upvote 0
Try moving the PlatformLegend Intialize event code from Intialize to Activate.

Part of your problem is from your using run-time controls. If PlatformLegend's Label was created at design time, your problem wouldn't exist.
 
Upvote 0
Try moving the PlatformLegend Intialize event code from Intialize to Activate.

Part of your problem is from your using run-time controls. If PlatformLegend's Label was created at design time, your problem wouldn't exist.

I changed
Code:
Sub Userform2_Initialize()

to

Code:
Sub Userform2_Activate()

and the same result was returned.
Unfortunately, all of the controls intended for PlatformLegend are conditional (I'm sorry that I didn't include that in my sample code above), and therefore, must remain created during run-time :(
 
Upvote 0
I just caught it. The code for the initialize event is put in the Sub Userform_Initialize() , no matter what the name of the user form. Remove the 2 from the name of the sub.


Rather than creating controls at run-time, its far more stable to create them at design time and make the necessary ones visible as needed. Linking event code to run time controls is a bit unstabalizing unless you use Class modules.
 
Upvote 0
I mistakenly thought VBA aggregated all userforms in the project, which necessitated the ordinal. You, sir, are a scholar and a gentleman! Your solution worked like a charm. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,644
Members
449,461
Latest member
kokoanutt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top