Multiple Instances of Form

T

Toby

I have a form which I use to access records from an
underlying table. These records have a field which
contains the year of the record. I would like to be able
to open multiple instances of this form so that I can
compare records form one year to the other.

I know that I could use multiple forms with the record
source set as a query which pulls only that year's data.
I would then have to have multiple individual forms along
with multiple queries.

I need to open form instances from a switchboard. I do
not need to use a command button on a form to open the
other instances.

Any help would be greatly appreciated.
 
T

Tim Ferguson

I need to open form instances from a switchboard. I do
not need to use a command button on a form to open the
other instances.

This is untested air code, but should give you an idea

' Put this at global level in a public module
'
' They have to be global, because the forms will be unloaded
' as soon as the variables go out of scope, otherwise.
'
Global dim g_frm(4) as Form_frmYearRecord


' and put this in a sub or something:
'
For w = 1 to 4
' create a new form object
Set g_frm(w) = New Form_frmYearRecord
With g_frm(w)
.Filter = "RecordYear = " & 2000+w
.Refresh ' ? should be requery

.Visible = True

End With

Next w


' and somewhere else
For w = 1 to 4
' the user may have already cancelled the form
' so check before trying to close it automagically
If g_frm(w) Is Not Nothing Then
g_frm(w).Unload '
Set g_frm(w) = Nothing

End If

Next w

Hope that helps


Tim F
 
E

Elwin

I believe the only way you can have multiple instances of
the same form visible at the same time is via 2 or more
sub-form controls on an 'unbound' master form that refer
to the same subform. If screen space is an issue consider
placing the subform controls onto separate pages of a tab
control. good luck.
 
A

Allen Browne

Easy enough to create multiple instances with the New keyword.

Managing them independently is another issue. See:
Managing Multiple Instances of a Form
at:
http://allenbrowne.com/ser-35.html
for an explanation and downloadable example.

(Pretty unusual doing this with a switchboard form.)
 
A

Albert D. Kallal

Elwin said:
I believe the only way you can have multiple instances of
the same form visible at the same time is via 2 or more
sub-form controls on an 'unbound' master form that refer
to the same subform.

No,not true at all. You can have multiple instances of the same form opened
many times. So, even if you have a complex customer invoice form with
several sub-forms, and the phone rings, you can minimize the current
customer, and then launch another copy of the SAME form. You are thus
allowed to have multiple copies of the same form opened many times.

This is often a great feature you can add your applications. You do have to
write code to do this.

If you want to try this ( and I am posting this for the original poster also
of course!).

Create a new blank form. This is the form that we will use to "launch" the
multiple copies of the form.

In this un-bound form, we go:

Option Compare Database
Option Explicit

Dim colFormsOpen As New Collection


Now, place a buttion on this form..and to use multiplate copies of a form
called "form1", you use:

Dim f As New Form_Form1
Dim strHead As String

strHead = "MyForm" & colFormsOpen.Count + 1

f.Caption = strHead
f.Visible = True

colFormsOpen.Add f, strHead


Try clicking on this button...you will get multiple copies of form1 (not
100% sure...but that form1 MUST have SOME code in it).

Also, to really cook you mind...open a bunch of forms..then try closing the
above form we just made (all of the forms you just opened will close...since
our collection goes out of scope...really cool!).
 
E

Elwin

great idea to use a collection, and now I understand what
the 'New' keyword does too. It's always been one of those
mystery fringe topics for me. Thanks!
 
G

Graham R Seach

Allen,

I had a look at your ser-35 page and noticed that you use the hWnd in the
collection. Till now I've been using a GUID, but I think the handle is
easier, and certainly guaranteed to be unique. I think I'll switch to using
that from now on. Thanks.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top