Collection needed to manage multiple instances of a form?

M

Max Moor

Hi All,

I have a single base form that I want to open multiple copies of at the
same time, each one filtering the base data a different way.

I note that the problem with just using the 'New' call to open a second
instance of a form is that when the first form is closed, the form it opened
also goes out of scope and disappears.

Allen Browne has a good write up of using a public collection to keep
client forms in scope, even after the creator form is closed.

In my application, I have what I call a "LeftNav" form which opens at
startup time and is always open. The user can't close it (at least without a
decompiler). I began thinking I would place my collection of forms there,
and have the LeftNav manage it all.

Then it occured to me (finally)... If the LeftNav is the only form
allowed to open or close these other "multi-forms," and the LeftNav itself
never closes, do I even need a collection to manage these?

I don't think I do, but I seem to miss the little things when I go
slogging through new territory sometimes. Then I have an epiphany and get
all cocky. I thought I should ask... just for a sanity check. :)

Thanks & Regards,
Max
 
A

Allen Browne

Others may have suggestions for you, Max, but FWIW, what I do is set the On
Close property of all forms and reports in the database (except the
'switchboard' or 'LeftNav') to:
=Keep1Open([Form]
or for reports:
=Keep1Open([Report])
Then if the user closes all visible forms and reports, the switchboard opens
again.

Actually, it's even easier than that: I actually use default forms and
reports that have these things already set:
http://allenbrowne.com/ser-43.html

Public Function Keep1Open(objMe As Object)
On Error GoTo Err_Keep1Open
'Purpose: Open the Switchboard if nothing else is visible.
'Argument: The object being closed.
'Return: None.
'Usage: In the OnClose property of forms and reports:
' =Keep1Open([Form])
' =Keep1Open([Report])
Dim sName As String 'Name of the object being closed.
Dim lngObjType As Long 'acForm or acReport
Dim bFound As Boolean 'Flag not to open the switchboard.
Dim frm As Form 'an open form.
Dim rpt As Report 'an open report.

'Initialize
sName = objMe.Name
If TypeOf objMe Is Form Then
lngObjType = acForm
ElseIf TypeOf objMe Is Report Then
lngObjType = acReport
End If

'Any other visible forms?
For Each frm In Forms
If frm.Visible Then
If frm.Name <> sName Or lngObjType <> acForm Then
bFound = True
Exit For
End If
End If
Next

'Any other visible reports?
If Not bFound Then
For Each rpt In Reports
If rpt.Visible Then
If rpt.Name <> sName Or lngObjType <> acReport Then
bFound = True
Exit For
End If
End If
Next

'If none found, open the switchboard.
If Not bFound Then
DoCmd.OpenForm "frmSwitchboard"
End If
End If

Exit_Keep1Open:
Set frm = Nothing
Set rpt = Nothing
Exit Function

Err_Keep1Open:
If Err.Number <> 2046& Then 'OpenForm is not available when closing
database.
Call LogError(Err.Number, Err.Description, conMod & ".Keep1Open()")
End If
Resume Exit_Keep1Open
End Function
 
B

bcap

You need one form object variable for every instance of the form that is
created. Unless you want to impose a predetermined limit on the number of
instances that can be created (by declaring a fixed number of object
variables), you are going to need to store these object variables in a
structure to which you can add new members indefinitely i.e. a collection.
Same logic applies regardless of the object from which you instantiate the
new form instances.
 
M

Max Moor

You need one form object variable for every instance of the form that is
created. Unless you want to impose a predetermined limit on the number of
instances that can be created (by declaring a fixed number of object
variables), you are going to need to store these object variables in a
structure to which you can add new members indefinitely i.e. a collection.
Same logic applies regardless of the object from which you instantiate the
new form instances.

Hi bcap,

I guess that makes sense. (darn it :) You're right in that I don't
want to have a predetermined number of them. There's no telling how many
someone may think they need open at once. Thanks for the help.

Regards,
Max
 
M

Max Moor


I need to spend more time on your site! I never heard of this before. I've
been sort of using a default form of my own. It's really fust the first form
I put together that functions with the LeftNav. I copy it every time I add a
new form to get the basic code setup. Of course, I have to go through and
strip out all the stuff from that form that doesn't apply to the new one, and
that's a time waster every time. I'll look at your default forms page some
more.

Thanks,
Max
 

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