Solved

Creating new forms from code

Posted on 2003-11-11
3
193 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

706 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

18 Experts available now in Live!

Get 1:1 Help Now