Putting controls on form

Posted on 2000-04-19
Last Modified: 2010-05-02
I need to put some controls on form in Visual Basic at run time. How do I do it?
Question by:sshayevich
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
  • 4
  • 2
  • 2
  • +2

Expert Comment

ID: 2731513
Add the first control, and give it an index of zero. (i.e. cmdTest(0)

Then, at runtime you can use the load method to load a new control:

Load cmdTest(1)

Then you have to postion it (it is loaded in exactly the same postion as the control with an index of zero) and set the visible property to true in order to see it.

Accepted Solution

Ruchi earned 100 total points
ID: 2731515
You need to delcare a variable to hold the reference to the control. Use the add method to create the instance of the control. Set the properties of the control to position it on the form and make it visible to you.

Try something like this...

Private Sub Command1_Click()
Dim otxtnew As TextBox

Set otxtnew = Controls.Add("VB.Textbox", "otxtNew")
With otxtnew
..Visible = True
..Text = "New Control"
..Width = Me.TextWidth(.Text) + 200
..Height = Me.TextHeight(.Text) + 50
..Top = 100
..Left = 300
End With
End Sub
LVL 28

Expert Comment

ID: 2731516
At design time give the control an index of 0 so as to create a control array. Then when you want to load a new instance of it, do it like this:

Textbox for example

Load Text(1)
Independent Software Vendors: 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 28

Expert Comment

ID: 2731520
geez i type slow

Expert Comment

ID: 2731576

Expert Comment

ID: 2731605
place all the controls you need on your form when building it an put the visible property at false then at run time change the visible at true for the control you need to use
LVL 28

Expert Comment

ID: 2731619
gfaucher...the question was how to add controls at run time.

Author Comment

ID: 2731651
When I try to add a new control to controls collection of the form, I don't get a list of properties and methods where I can pull the Add method from. Any idea why?
LVL 28

Expert Comment

ID: 2731670
if youre talking about the convenient popup menus that appear when you normally type in comments like text1.text (when you type the "."), that won't appear when referencing the control collection of the form. However it doesnt mean its not working.  It should still run fine.

Author Comment

ID: 2731732
Thank you!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
VBS file using code from 2nd file (txt or vbs) 4 48
MS SQL store procedure to calculate and return result 6 69
vbModal 12 66
Dinamic report to Crosstab query 9 53
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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