multiple instances of one form?

D

djc

if a form is a class and you can have multiple instances of a class can you
have multiple instances of a form?

Let say I want to write a class that does a particular thing... like open a
form and fill a listbox on it. However, from within that form a user can
double click on an item in the listbox to launch another form with a listbox
containing 'sub-items' from the 'parent' form... from that form the cycle
could continue indefinitely.

Basically I want to write an app to track/organize tasks. Tasks could have
sub-tasks.. and those sub-tasks could have sub-tasks.. etc.. I don't want to
put a limit on how many levels deep you could go.

Can I write a class that does this? I obviously can't create an endless
number of forms ahead of time. And the form is the same for each sub-level
of tasks that would be opened.. only the source of the listbox and some
labels would change with each succesive form open.

Or should I just use one form and change the source each time so there is
only one form showing at any one time? These are still just ideas right
now.. I'm looking for some input before I implement.

Thanks in advance for any thoughts
 
S

Sandra Daigle

You can open multiple instances of a form -

For each new instance you need an form object variable which you set using
the New keyword. Once you have the form open you can change it's
recordsource and caption.

Be careful

- the form that you are instancing must have a module (ie it can not be a
lightweight form).

- the form object variable is the best way to refer to this new instance
since you will not be able to find the form in the forms collection (unless
you know it's index).

- As soon as your object variable goes out of scope, the new instance of the
form will automatically be destroyed (and closed).

Here is some sample code that uses the selected rows in a listbox to open
multiple instances of the customer form.

Note that col is declared in the Module header so that it stay's in scope
until the current module terminates.

Dim colForms As Collection
Private Sub Command2_Click()
Dim varItem As Variant
Dim frm As Form
Set colForms = New Collection
For Each varItem In Me.List0.ItemsSelected
Set frm = New Form_Customer
frm.RecordSource = "Select * from Customer " _
& "Where Custid=" & Me.List0.ItemData(varItem)
frm.Caption = "Customer: " & Me.List0.Column(1, varItem)
colForms.Add frm
Next varItem
End Sub

You might also want to look at my MultiInstanceForm example on
http://www.daiglenet.com/msaccess.htm

Last but not least, here are some articles on this topic:


ACC2000: How to Open Multiple Instances of a Form
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q210248

ACC: How to Open Multiple Instances of a Form
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q135369
 
D

djc

Thanks for all the great info.. I'll look into each of these.

Sandra Daigle said:
You can open multiple instances of a form -

For each new instance you need an form object variable which you set using
the New keyword. Once you have the form open you can change it's
recordsource and caption.

Be careful

- the form that you are instancing must have a module (ie it can not be a
lightweight form).

- the form object variable is the best way to refer to this new instance
since you will not be able to find the form in the forms collection (unless
you know it's index).

- As soon as your object variable goes out of scope, the new instance of the
form will automatically be destroyed (and closed).

Here is some sample code that uses the selected rows in a listbox to open
multiple instances of the customer form.

Note that col is declared in the Module header so that it stay's in scope
until the current module terminates.

Dim colForms As Collection
Private Sub Command2_Click()
Dim varItem As Variant
Dim frm As Form
Set colForms = New Collection
For Each varItem In Me.List0.ItemsSelected
Set frm = New Form_Customer
frm.RecordSource = "Select * from Customer " _
& "Where Custid=" & Me.List0.ItemData(varItem)
frm.Caption = "Customer: " & Me.List0.Column(1, varItem)
colForms.Add frm
Next varItem
End Sub

You might also want to look at my MultiInstanceForm example on
http://www.daiglenet.com/msaccess.htm

Last but not least, here are some articles on this topic:


ACC2000: How to Open Multiple Instances of a Form
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q210248

ACC: How to Open Multiple Instances of a Form
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q135369

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

if a form is a class and you can have multiple instances of a class
can you have multiple instances of a form?

Let say I want to write a class that does a particular thing... like
open a form and fill a listbox on it. However, from within that form
a user can double click on an item in the listbox to launch another
form with a listbox containing 'sub-items' from the 'parent' form...
from that form the cycle could continue indefinitely.

Basically I want to write an app to track/organize tasks. Tasks could
have sub-tasks.. and those sub-tasks could have sub-tasks.. etc.. I
don't want to put a limit on how many levels deep you could go.

Can I write a class that does this? I obviously can't create an
endless number of forms ahead of time. And the form is the same for
each sub-level of tasks that would be opened.. only the source of the
listbox and some labels would change with each succesive form open.

Or should I just use one form and change the source each time so
there is only one form showing at any one time? These are still just
ideas right now.. I'm looking for some input before I implement.

Thanks in advance for any thoughts
 

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