Consulting

Results 1 to 7 of 7

Thread: Adding Controls to Pages programatically

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Adding Controls to Pages programatically

    I am using this code to add Controls to My User Form "PivotTableOptions"

    [VBA]LC = "ItemCode"
    NO = "NoItemNumber"
    SBI = "SortbyItem"
    SBP = "SortbyProduct"
    PivotTableOptions.Controls.Add bstrProgId:="Forms.Frame.1", Name:=LC, Visible:=True
    PivotTableOptions.Controls(LC).Top = 168
    PivotTableOptions.Controls(LC).Left = 2
    PivotTableOptions.Controls(LC).Height = 70
    PivotTableOptions.Controls(LC).Width = 210
    PivotTableOptions.Controls(LC).Caption = "Do you want Item# in the Report?"
    PivotTableOptions.Controls(LC).Font.Bold = True
    PivotTableOptions.Controls(LC).Add bstrProgId:="Forms.optionbutton.1", Name:=SBP, Visible:=True
    PivotTableOptions.Controls(SBP).Top = 14
    PivotTableOptions.Controls(SBP).Left = 12
    PivotTableOptions.Controls(SBP).Height = 14
    PivotTableOptions.Controls(SBP).Width = 120
    PivotTableOptions.Controls(SBP).Caption = "Yes, Sort by Product"
    PivotTableOptions.Controls(SBP).Font.Bold = True
    PivotTableOptions.Controls(SBP).GroupName = Itemcode


    PivotTableOptions.Controls(LC).Add bstrProgId:="Forms.optionbutton.1", Name:=SBI, Visible:=True
    PivotTableOptions.Controls(SBI).Top = 38
    PivotTableOptions.Controls(SBI).Left = 12
    PivotTableOptions.Controls(SBI).Height = 14
    PivotTableOptions.Controls(SBI).Width = 120
    PivotTableOptions.Controls(SBI).Caption = "Yes, Sort by Item#"
    PivotTableOptions.Controls(SBI).Font.Bold = True
    PivotTableOptions.Controls(SBI).GroupName = Itemcode

    PivotTableOptions.Controls(LC).Add bstrProgId:="Forms.optionbutton.1", Name:=NO, Visible:=True
    PivotTableOptions.Controls(NO).Top = 27
    PivotTableOptions.Controls(NO).Left = 140
    PivotTableOptions.Controls(NO).Height = 14
    PivotTableOptions.Controls(NO).Width = 30
    PivotTableOptions.Controls(NO).Caption = "No"
    PivotTableOptions.Controls(NO).Font.Bold = True
    PivotTableOptions.Controls(NO).Value = True
    PivotTableOptions.Controls(NO).GroupName = Itemcode
    [/VBA]

    However, Now I ran out of room on that form, so I added Pages to it. Now I want to use that code to add it to the page named "ReportSetup". I can't get it to work I thought I would put

    [VBA]PivotTableOptions.ReportSetup.Controls[/VBA]

    etc.. But that didn't work

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why would you want to add controls via code. It is not a good idea, and incredibly inefficient.

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I am adding the controls programatically because they should only appear if the user chooses other options

  4. #4
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [VBA]TextBox1.Visible = False[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    In part, what XLD is referring to here is that just because something CAN be done does not necessarily mean that it SHOULD be done as a matter of course or that it is the best option.

    The course that Steve has alluded to would be the better option... You can have almost any number of controls on a form, just set their visible property to false until the conditions are met where you want them shown.

    Another alternative would be for the extra controls to be (say) added onto the RHS of a form of width 2 * X, and when the form is activated set the form width as either X, or, 2 * X if your conditions to show the extra controls are met. Or maybe you'd want to show it as width X, and set the width as 2 * X when (say) a button is clicked...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by johnske
    In part, what XLD is referring to here is that just because something CAN be done does not necessarily mean that it SHOULD be done as a matter of course or that it is the best option.

    The course that Steve has alluded to would be the better option... You can have almost any number of controls on a form, just set their visible property to false until the conditions are met where you want them shown.

    Another alternative would be for the extra controls to be (say) added onto the RHS of a form of width 2 * X, and when the form is activated set the form width as either X, or, 2 * X if your conditions to show the extra controls are met. Or maybe you'd want to show it as width X, and set the width as 2 * X when (say) a button is clicked...
    Exactly.

    It is far better to have the controls and just tuck them away and bring them into play when required rather than adding them at run time. Remember also that if the control does not exist when the form is designed, there will be no event code for it. You are then into territory of either adding event code on the fly, not trivial with forms, and/or creating pseudo-control arrays. Either way, you are adding another level of complexity to the application which just is not warranted. I think I can safely say that I have NEVER built a commercial application that builds controls on the fly, just not necessary.

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thank you I never thought of that way. That does seem to be more efficent. I never knew about that way. I am learning, be patient with me.

    Daniel

Posting Permissions

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