Link to home
Start Free TrialLog in
Avatar of zionz
zionz

asked on

Creating objects at runtime

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
Form2.Show

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

Form2.text1="Test"
Form2.Show

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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"
ff.Show
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.

Avatar of MilanKM
MilanKM

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.

Thanks
MilanKM