[AccessD] Multiple Form Instances

JWColby jwcolby at colbyconsulting.com
Tue Oct 10 06:46:41 CDT 2006


John,

Not exactly...

A new form is opened two ways.  

1) Docmd.Openform "formname" is the one everyone knows.  This does NOT give
you a direct handle but can be found in the Forms("Formname").
2) The other way is to dimension a variable of the type form

Dim frm as form
	set frm = new form_FormName.  

Frm now contains a pointer to the form formname.

This whole process is a shortcut if you will, form_ simply tells VBA to look
in the documents collection where the CLASSES for forms are kept and then
use the part after the underscore as the NAME of the form class to open.
Opening a class opens the form.

While we are on the subject, I am guessing that if the form has the "no
module" property set, then it has no class and this method will not work.

So.... To reiterate:

Dim a variable of type form:

Dim frm as form

SET that variable to the CLASS for a specific form:

	set frm = new form_MyForm

You now have an OPENED form and a pointer to that form holding it open.  NOW
store that pointer into a collection using some name to index into the
collection:

Dim colMyForms as collection

	set colMyForms = new collection
	colMyForms.add frm, "MyFormName1"

	set frm = new form_MyForm
	colMyForms.add frm, "MyFormName2"

	set frm = new form_MyForm
	colMyForms.add frm, "MyFormName3"

You can now reference any specific form by getting the pointer to the form
back out of the collection:

Dim frm as form
	set frm = colMyForms("MyFormName1")
	frm!MyControl.Value = "Hello"

OR... You can use it in situ...

	colMyForms("MyFormName2")!MyControl.Value = "Hello again"

To summarize. A form can be opened more than one time, however only by
creating pointers to the form and STORING those pointers somewhere.  A
collection that we create for the purpose is a good place to save them, but
it can also be just a dimensioned variable of type form, somewhere in your
code.  

IMPORTANT!!! You MUST cleanup behind yourself in order to close such a form.
You can apparently close the form by clickint the close control in the
form's upper right, however it leaves stuff in VBA if you don't also delete
the POINTER to the form in the collection or in the variable that you dimmed
to hold the form pointer.

John W. Colby
Colby Consulting
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of John Skolits
Sent: Tuesday, October 10, 2006 1:34 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

Oh, I see. If you create a collection from within a form and then ad a new
form, it makes a new form of the one you're in. I get it.

Thanks for your help.

John


-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Monday, October 09, 2006 11:12 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

John,

No, you don't need to design a new form from scratch by code.
You can use your form - just set its HasModule property to True if it
doesn't have any code behind.

--
Shamil
 
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of John Skolits
Sent: Tuesday, October 10, 2006 6:12 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

I think that all makes pretty good sense.

But, will I have to actually design the new form from scratch through code?

My plan was to just grab an existing form and open it up many times just
changing the recordsource for each instance.

John




-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Monday, October 09, 2006 7:31 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

John,

You cannot refer to such form instances by name - you have to use indexes in
global Access.Application.Forms collection or in your custom collection if
you put form's instances object references in it as I have shown in the
code. BTW, you HAVE to put form instance's object reference somewhere after
you open it "object way" (as shown in the code) otherwise it will be closed
immediately after your code leaves the sub/function where form is opened
this object way except the case when your form is modal - for this latter
case your code will "get stuck" at the point:

frm.visible = true

If you wanted to refer to your forms' instances using mnemonic names like
e.g:

- PriceByDefectiveMaterialForm
- PriceByDefectiveSolderForm
- PriceByDefectiveWiringForm
- ...

Then you can use these mnemonic names while putting forms' instances object
refs into collection:

col.add frm, "PriceByDefectiveMaterialForm"
....

... and then somewhere in other part of your code ...
Set frm = col("PriceByDefectiveMaterialForm")

etc.

Of course col collection should be (custom class) module level variable...

--
Shamil
 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of John Skolits
Sent: Tuesday, October 10, 2006 1:52 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

Then can I refer to the form object"frm" to set it's individual properties?

Is there a way to then name that form object in case I have a few open and
want to make change in one particular form?

Or will they just be enumerated frm(0), frm(1) etc?

John


-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Monday, October 09, 2006 5:49 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Multiple Form Instances

Yes, John, you can have multiple form instance opened at once - just make
sure they have a form module or HasModule property set to true and then use
(let's say your form's name is myForm):

Dim col as new collection
Dim frm as new myForm

col.add frm
frm.visible = true

Similar way multiple instances of reports can be opened.


--
Shamil
 
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of John Skolits
Sent: Tuesday, October 10, 2006 1:30 AM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Multiple Form Instances


I think there is a way to do this.

Can I have multiple instances of the same form.

I have form with a chart on it. I want to show multiple instances of this
same form but I'll provide a different data source for the chart.

This way if I need to do:

Price By Defective Material
Price by Defective Solder
Price By Defective Wiring
....

(About 20 options) 

I don't need 20 forms.
I still want to give them the ability to have any number on the screen at
the same time.

I'm also guessing if I can do this forms, I should be able to do this with
reports as well.

John



--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list