Solved

How to dynamically create controls

Posted on 2004-10-08
10
147 Views
Last Modified: 2012-05-05
Please help:

I am trying to make a billing form.

I need to dynamically create lines of controlls.  Does anyone know how to do this?

I need to create a new line of controls once the previous line has been set, so that a person may specify an infinate number of items and other variables.

Thanks for the help.
0
Comment
Question by:antaren
10 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 168 total points
ID: 12258924
The best way to do this is place one of the controls you want to use on the form and set its Index property to 0.  This creats a control array.  To add other controls to the array at runtime, you call Load ControlName(i).

Here is an example using a TextBox (your form already has Text1(0) placed on it.

Dim i as Long

For i = 1 to 9
    Load Text1(i)
    Text1(i).Top = Text(0).Top + (Text(0).Height * i )
    Text1(i).Visible = True
Next

Hope it helps!
0
 
LVL 6

Assisted Solution

by:bkthompson2112
bkthompson2112 earned 166 total points
ID: 12258942
Hi antaren,

Here's another way using the Controls.Add method.

http://www.codeguru.com/vb/controls/vb_activex/activex/article.php/c3499/

bkt
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12259050
The advantage of using the control array is that you can easily receive the controls events.  e.g. in a dynamic TextBox control array you only need one Validate event for all the newly created controls in the array:

Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
    'Use the Index property to determine which control fired the event
    Debug.Print "TextBox # " & Index & " Validate event"
 
    If Text1(Index).Text = "" Then
        MsgBox "Cannot be empty"
        Cancel = True
    End If
End Sub

 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 12259051
>>I need to create a new line of controls once the previous line has been set, so that a person may specify an infinate number of items and other variables.


Have you think about to use listview to show data instead?
0
 

Author Comment

by:antaren
ID: 12263566
EDDYKT,
The list view would only be appropriate if it displays the selected items for the bill.  It may or may not be easier to set it up that way.  If I were using the list view, I would use one set of controls to select an item, and an ADD button that would place the selected item, quantity and price into the list box.

Erick37,
That is a nice trick, unfortunately the controls need to be created dynamically.  Even though your method would work, I would need to create each line of controls, which is what I'm trying to avoid in the first place.  The bill can have many items on it, and even if I created an arbitrary number (such as 50 lines) the user may still reach the limit.

bkthompson2112,
This method does look interesting, is there any way to use the controls.add method to create controls repeatedly on a list?  Can the controls be represented individually, specifically they each can contain a differant value?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12264004
Hi antaren:

You only need to create one line of controls during design time.  Each control in the line has its Index set to 0.  During runtime you would load lines 2 thru n.
0
 
LVL 10

Assisted Solution

by:anv
anv earned 166 total points
ID: 12265004
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now