Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using VBA to add controls (programmatically) (VBA for Word 2000)

    I have a "For each ..." that I need to add controls for as follows:

    Issue 1:
    For each item that matches the criteria, I need to add two labels that match two variables that would have been set. I know (generically) how to set the caption once they are there, but am not sure how to have VBA create the labels.

    Issue 2:
    For each item that matches the criteria, I need to add a couple of combo boxes. The values in the combo boxes will be the same for each item.

    To sum, for each item that matches the criteria I need to create the following row of controls on my form:

    Label1 Label2 Combo1 Combo2

    (Obviously each label and combo box would have to have diffferent names for each row).

    Thanks for your help!!
    Troy

  2. #2
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBA to add controls (programmatically) (VBA for Word 2000)

    Hi Troy,

    I think that the Controls collection of the UserForm is what you need. Here is an example that uses a command button to add a new label, named lblPhoneNumber, making it visible, then setting the appropriate properties:

    Dim DesiredControl As Control
    Private Sub cmdAddControls_Click()

    Set DesiredControl = Controls.Add("Forms.Label.1", lblPhoneNumber, Visible)
    DesiredControl.Left = 18
    DesiredControl.Top = 100
    DesiredControl.Width = 175
    DesiredControl.Height = 20
    DesiredControl.Caption = "Phone No."

    End Sub

    To add a combobox the first parameter to the add method would be "Forms.Combobox.1". You can find more details in the Help topic of "Controls Collection". Have fun!

  3. #3
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBA to add controls (programmatically) (VBA for Word 2000)

    Hi again Troy,

    One more thing that might help. Use the Controls.Add method to create and position the controls but don't try to add the items to the combobox right then and there. You can handle populating both comboboxes with the same data in the UserForm's AddControl event which will fire after each control is added. If the type of control just added is a combobox just fill it with the values needed.

    Private Sub UserForm_AddControl(ByVal Control As MSForms.Control)
    If TypeOf Control Is ComboBox Then
    Control.AddItem "Lounge"
    Control.AddItem "Lex"
    Control.AddItem "Lizard"

    End If

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •