Solved

How to create objects dynamically with unknow number of them

Posted on 2004-10-13
6
229 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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