?
Solved

How to create objects dynamically with unknow number of them

Posted on 2004-10-13
6
Medium Priority
?
232 Views
Last Modified: 2012-05-05
I have an example of how to create a combo box dynamically, but I want to create multiple of it with the number of it from an input on run time.

Here an example of creating a combo box on run time:

Dim WithEvents cboControl As ComboBox
Set cboControl = Controls.Add("VB.ComboBox", "cboControl")

but how to create more than one? Please give code.
0
Comment
Question by:limct
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297081
It would probably be easier to use a control array:

Place a combobox on your form
set its index to 0 and Visible = False

When you need to add a new one:

        lIndex = cboControl.Ubound
        Load cboControl(LIndex)
        With cboControl(LlngIndex)
            .Container = ...
            .ToolTipText = ...
            .TabStop = True
            .TabIndex =..
            .Left = ...
            .Top = ...
            .Visible = True
        End With



0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297108
Oops...
        Load cboControl(cboControl.Ubound + 1)
        With cboControl(cboControl.Ubound)
            .Container = ...
            .ToolTipText = ...
            .TabStop = True
            .TabIndex =..
            .Left = ...
            .Top = ...
            .Visible = True
        End With

Fill in ...s with your stuff
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12297138
I would certainly be better to use an array. But if you still don't want to use one, here's what you can do...

------------------------------------------------------------------------------------------------------
Dim WithEvents cboControl As ComboBox
Dim Num as Integer
Dim I as Integer

Num = InputBox("How many combo boxes to add","How Many")

For I = 1 To Num
     Set cboControl = Controls.Add("VB.ComboBox", "cboControl" & Num)
Next I
------------------------------------------------------------------------------------------------------

Hence you'll have new controls as cboControl1, cboControl2, cboControl3, cboControl4, etc...

But once again, it'll be better to use Arrays since the main control name will be constant and it provides many other advantages. Hope this answers your question :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 2000 total points
ID: 12297182
Try looking at http://vbcity.com/forums/topic.asp?tid=4953

There was someone there trying to do the same thing with text boxes.  It appears you cannot create a control array at runtime but a poster there suggested that you can wrap them in a class...

HTH

J.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297230
eeshahidt:

Yes, but in your example the events for all but the last added control will be lost. The WithEvents clause only works for cboControl. In this case cboControl refer to the last control added in the For loop.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12297472
Oh! Well glad you got your answer. I learnt a little as well ;)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question