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.

zionzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
If you are creating instances of Form1, then declare your variable as such...

   Dim f1 As New Form1       '  <--- "Form1" not "Form"
   f1.Text1 = "Text"
   f1.Show
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iHadiCommented:
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
0
PockyMasterCommented:
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.

0
MilanKMCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.