?
Solved

Creating new forms from code

Posted on 2003-11-11
3
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
Crazy_king_in earned 140 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

800 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