title: Referencing Control Objects Created in Different Module
body: <p>dr (no circumstantial details): I am trying to reference a control created programmatically in another module, but it is throwing </p>
  
  <blockquote>
    <p>"Compile Error: Variable not defined."</p>
  </blockquote>
  
  <p>Here's what I'm trying to do (and perhaps my approach is wrong entirely, if so, let me know): I am trying to create a userform that uses a listbox to act as a menu in which the user can select an item and see the controls (labels, textboxes) relevant to that item. For instance: item "General Information" would a submitter name, department, date, etc; item "Report request" would have report application, desired app name, etc. Selecting "General Information" on the listbox should .visible=true the frame housing the "GI" controls and hide other frames, and selecting "Report Request" item would .visible=false General Information frame but .visible=true Report Request frame, and so on.</p>
  
  <p>Because I want the controls to be placed in the same place on the userform, I created them all programmatically in a Construct module. It looks like this:</p>
  
  <pre><code>Public Sub GeneralInformationCreator()
  
  Dim i As Integer
  Dim labelGeneral As MSForms.Label
  Dim frameGeneral As MSForms.Frame
  Dim frameRequestInformation As MSForms.Frame
  Dim frameSubmitterInformation As MSForms.Frame
  Dim labelAppSelect As MSForms.Label
  Dim listAppSelect As MSForms.ListBox
  Dim labelDateSelect As MSForms.Label
  Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
  Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
  Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
  Dim labelSponsorFName, labelSponsorLName As MSForms.Label
  Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
  Dim labelDepartmentName As MSForms.Label
  Dim inputDepartmentName As MSForms.TextBox
  
  Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
      With labelGeneral
          .Font.Size = 12
          .Top = 12
          .Left = 192
          .Height = 14.25
          .Width = 42
          .Caption = "General Information"
          .Name = labelGeneral
      End With
  
  Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
      With frameGeneral
          .Top = 30
          .Left = 192
          .Height = 310
          .Width = 384
          .Caption = ""
          .BorderColor = RGB(255, 255, 255)
  
          Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
              With frameRequestInformation
                  .Top = 15
                  .Left = 15
                  .Height = 100
                  .Width = 350
                  .Caption = "Request Information"
                  .BorderStyle = 1
                  .BorderColor = RGB(0, 0, 0)
  
                  Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                      With labelAppSelect
                          .Caption = "Select an application:"
                          .Top = 15
                          .Left = 15
                          .Width = 100
                          .Height = 20
                          .AutoSize = True
                      End With
  </code></pre>
  
  <p>The Construct methods are called at runtime using UserForm_Initialize() like this:</p>
  
  <pre><code>Private Sub UserForm_Initialize()
  
  Call Construct.GeneralInformationCreator
  Call Construct.ApplicationDetailsCreator
  
  With formRequestWizard.listMenu
      .AddItem ("General Information")
      .AddItem ("Application Details")
      '.Selected(0) = True
  End With
  
  End Sub
  </code></pre>
  
  <p><strong>Here's the issue:</strong> in the userform code, I have a listMenu_Change() that looks like this:</p>
  
  <pre><code>Private Sub listMenu_Change()
  
  If (listMenu.Selected(0) = True) Then
      labelGeneral.Visible = True
      frameGeneral.Visible = True
  Else
      labelGeneral.Visible = False
      frameGeneral.Visible = False
  End If
  
  If (listMenu.Selected(1) = True) Then
      labelAppDetails.Visible = True
      frameAppDetails.Visible = True
  Else
      labelAppDetails.Visible = False
      frameAppDetails.Visible = False
  End If
  
  End Sub
  </code></pre>
  
  <p>Selecting an item from the listbox throws the error from the tl;dr: "Compile Error: Variable not defined" on the first variable "labelGeneral". </p>
  
  <p>I have tried putting all of the script into the userform code window, and putting the control Dims outside of Subs. The research I've done has mostly led to discussions about how to create event handlers for controls created at runtime, but I've decided to place the listMenu object via the object view so I'm not sure that's applicable.</p>