Solved

Creating new forms from code

Posted on 2003-11-11
3
194 Views
Last Modified: 2010-05-03
Hi!

How can I create new forms or any other objects only from code?

I have seen somewhere, that this can be done by using New keyword, but I don't know how exactly it works.

This is what I have so far:

    Dim form2 As New Form1
    Load form2
    form2.Show

This creates new form which looks like form1, but I want to create completely new and empty form.
Can I create an array of forms?      form(0), form(1), form(2) . . .
How do I change properties of new forms?    My example gives 'Object Required' error when I try:     form2.caption = "abcdefg"

If possible, can you give me code example that would do the following:

- create 2 new empty forms, and display them
- change caption for first form
- create button on second form

Thanx!
0
Comment
Question by:dbrckovi
3 Comments
 
LVL 1

Accepted Solution

by:
Crazy_king_in earned 35 total points
ID: 9721598
Create a new form named Form1

Have a command button, paste the following code in the click event of the command button, u can see what u want


Dim J As New Form1
Dim K As New Form1
J.Show
K.Show
J.Caption = "First Form - Empty"

K.Caption = "Second Form"
K.Move 0, 0
K.Controls.Add "VB.TextBox", "cmdObj1", K
With K!cmdObj1
    .Visible = True
    .Width = 2000
    .Text = "Dynamic TextBox"
End With
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9721764
You can have index for new forms.

Create a form called Form1.

Place whatever you nee inside

Then declare the following

Dim MyForm(x) as Form1 ' where x is a value, but you could also define it like Dim MyForm() as Form1 and then redim it


' index is a value from 0 to x-1
Set MyForm(index) = New Form1

Then you can access all objects as follow:


MyForm(0).label1.caption="Text Here"
MyForm(0).text1.text="Other Text"

In order to handle the form correctly I suggest you to have a control or a variable inside the original form the will be assigned with the index (in order to know exactly wich of the instance fired an event)

MyForm(0).labelindex.caption=0


Hope this help,

Max
0
 
LVL 11

Author Comment

by:dbrckovi
ID: 9731028
Thanx for replies!

Crazy_King_In

Your example works, but form J is not empty, it contains a Command Button.

I want to create a realy empty form, that would act exactly if it was created by 'Add New Form' from Design Mode.

I managed to simulate this by adding an empty form to my project (ftmTemplate), and then creating new forms as frmTemplate.   >>  Dim frmNewForm As New frmTemplate <<

Is there some more elegant way?

I mean, does it have to be created by a model (Form1)? can't it be created from scratch?
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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 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…

861 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

22 Experts available now in Live!

Get 1:1 Help Now