Results 1 to 6 of 6

Thread: Dynamically create Textboxes & Checkboxes in User Forms

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Dynamically create Textboxes & Checkboxes in User Forms

    I have created on user form. Within it is a dropdown that will prompt for a number. That integer saves to a variable which is public to the module.
    I have created a second user form. Within that user form I would like to dynamically create a number of textboxes based on the integer to which the aforementioned dropdown saved its value. Is there a method to create textboxes on the fly within user forms, or how would I make this happen?

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Dynamically create Textboxes & Checkboxes in User Forms

    You can create them dynamically, but have to set a lot of properties (top, left, etc), which could be a lot of work if the user has a lot of choices.
    VB Code:
    1. Dim t as textbox
    2. t.top = 1600
    3. t.name = "txtFoo"
    4. 'and so on
    or, you could create the controls you want, set visible = false, and set visible = true based on whatever the value is.
    Or, if the controls are the same but their values differ, just set those values when the form opens.
    Last edited by salvelinus; Jun 10th, 2005 at 05:07 PM.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Re: Dynamically create Textboxes & Checkboxes in User Forms

    Do I have to set all of the properties, or just the ones like location that would be specific to the control? Would the rest take defaults? And from looking at your response, am I correct in figuring then that all I have to do is declare a variable of that type? There isn't an add method to tell it that variable goes into a specific user form? How do I associate this with a specific userform if I want to get an answer from a dropdown in one form, which would show the input userform upon clicking in the aforementioned dropdown?

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Dynamically create Textboxes & Checkboxes in User Forms


  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Re: Dynamically create Textboxes & Checkboxes in User Forms

    I'm too much of a newbie at VBA to quite follow that one. I'm not sure where in my code I would be adding the text box and how I would be telling it to which user form the text box is being added.

    For the time being, I think it will be more efficient for me to go with your first suggestion and simply make the text boxes all invisible then use an iterative structure to change them to visible until the number the dropdown supplied is reached. However, that brings more questions up for me.

    I tried accessing the text boxes like so:

    Code:
    Dim BxName As String
    Dim ChkName As String
    Dim i As Integer
    'Num is a global integer
    
    For i = 1 To Num 
                            
                BxName = "NameBox" & CStr(Num)
                ChkName = "U18Chk" & CStr(Num)
            
                KidPrompt.BxName.Visible = True
                KidPrompt.ChkName.Visible = True
            
    Next Num
    But it doesn't let me replace the textbox name with a variable holding that name. How can I loop through the text & check boxes to make them visible so that it stops after the global Num value & only makes visible certain boxes (there are others that will be made visible if the checkbox is true)

  6. #6
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Dynamically create Textboxes & Checkboxes in User Forms

    Quote Originally Posted by kfcSmitty
    This will not work, there is no index in texbox property in VBA.


    Kaaja

Posting Permissions

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



Click Here to Expand Forum to Full Width