Creating objects at runtime

Posted on 2006-03-25
Last Modified: 2010-05-01
Hello, im trying to load multiple forms at runtime, currently im unsing a form as a template loading a copy of it when its needed, like this:

Dim Form2 as Form
Set Form2 = New Form1

It is working but the problem is i can't access the controls, there are some textboxes in form1 that i need to modify, im trying this but it doesn't works:

Dim Form2 as Form
Set Form2 = New Form1


Its possible to do such thing?,thnx in advance.

Question by:zionz
    LVL 85

    Accepted Solution

    If you are creating instances of Form1, then declare your variable as such...

       Dim f1 As New Form1       '  <--- "Form1" not "Form"
       f1.Text1 = "Text"
    LVL 13

    Expert Comment

    It should work with no problems regarding that all the controls anf forms you are using exists
    I tried the following and it worked

    Dim ff As Form
    Set ff = New Form2

    ff.Text1 = "dddd"
    LVL 14

    Expert Comment

    Yes, it will work, since Form1 is inherited by Form.
    But if you use the
    dim as Form
    set as new Form1 construction, you will not see the intellisense for form1 but for form in, and that will make you think you cannot access it.
    While actually, your object is of type Form1 which is a larger object than Form (at least functionality Form + added functionality of Form1)

    So you can call  the object you've added yourself.

    It's easier to do it the way Idle_Mind did it.. it will show you what you need, but  in other cases, it's easier to create a base object first, and create derived instances later.

    LVL 14

    Expert Comment

    objects can be created at run time. Controls, forms, and class modules are examples of objects which you can create both at design time & runtime by creating variable objects and collections.

    Use the DIM function to create a variable whose type is that of any registered class.

    such as Dim FORM1 as FORM

    Variables, defined as an object type are useful because they can refer to an existing object. For example

    Set X =NEW FORM1

    Look at the use of the Set statement. To establish a variable reference to an object, you cannot simply use the equal sign. You must have to use the SET statement.
    There are several reasons why you should be interested in creating a second reference to a control.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    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.
    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…
    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…

    729 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