469,343 Members | 5,186 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to create multiple instances of the same form?

sueb
379 256MB
Is there a way to specify that selecting a Switchboard option (that brings up a form) creates another instance of that form v. changing focus to the existing one?
Dec 1 '10 #1

✓ answered by ADezii

sueb, I've revised your Front End once again in order to demonstrate how you can create Multiple Instances of a Form (Patient_IUR_Overview) via a Switchboard, while using a Collection to manage them. You can now Open as many Instances of Patient_IUR_Overview as you like directly from the Switchboard, Close each Instance individually, or Close ALL Instances via the Close ALL Clients Option on the Switchboard. Pay close attention to:
  1. The Close() Event of the Patient_IUR_Overview Form.
  2. The Close ALL Clients Option on the Switchboard.
  3. The OpenAClient() Function.
  4. The CloseAllClients() Function.
  5. The Collection Declaration in the General Declarations Section of Module1.

42 7517
ADezii
8,800 Expert 8TB
Opening Multiple Instances of a Form can be a little tricky. This Demo should help you out.
Attached Files
File Type: zip Multiple Form Instances.zip (17.6 KB, 655 views)
Dec 2 '10 #2
sueb
379 256MB
Thanks for this. A couple of questions:

I'm assuming that the code in OpenAClient is what I'd need to implement? What is the purpose of the collection mentioned in that code? The names are a little cryptic, so I feel very uncertain about understanding the implications of what's going on in that module. What does a "caption" do for me?

Also, why is there a special module to close forms? Why can't the user just close whichever instance they want to?
Dec 2 '10 #3
ADezii
8,800 Expert 8TB
I'm assuming that the code in OpenAClient is what I'd need to implement?
Correctamundo!
What is the purpose of the collection mentioned in that code?
To hold and manage the various Form Instances.
The names are a little cryptic, so I feel very uncertain about understanding the implications of what's going on in that module. What does a "caption" do for me?
All Instances of a Form are exactly the same as far as Name goes, so the Caption is a method of telling one from another.
Also, why is there a special module to close forms? Why can't the user just close whichever instance they want to?
Yep, the User can manually Close an Instance he/she wants to.
Dec 2 '10 #4
sueb
379 256MB
As far as managing the form instances, what actions are we talking about?
Dec 2 '10 #5
sueb
379 256MB
Also, I don't seem to be able to add this module as a selection on the Switchboard, which is where I want it.
Dec 2 '10 #6
ADezii
8,800 Expert 8TB
The Switchboard Item calls the Public OpenAClient() Function.

P.S. - See Attachment.
Attached Files
File Type: zip Multiple Form Instances_2.zip (35.0 KB, 226 views)
Dec 2 '10 #7
sueb
379 256MB
I copied and pasted that code into my module (changing the name of the form to match mine) and added it to my Switchboard, but when I tried to execute it, the screen inside my Access window flashed (as though it were going to create the new form) and then nothing else happened. The Switchboard was still there, but there was no form.
Dec 2 '10 #8
ADezii
8,800 Expert 8TB
TO create an Instance of your Form:
  1. Remove all references to the Collection.
  2. Declare frm as Public in a Standard Code Module.
  3. Download the Attachment for a visual.
Attached Files
File Type: zip Multiple Form Instances_3.zip (40.5 KB, 244 views)
Dec 2 '10 #9
sueb
379 256MB
Well, I hate to be so dense, but it's still not working at all, just that flash. Here's what I have, and "OpenAClient" is on my Switchboard:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. 'Purpose:   Open an independent instance of form frmClient.
  3. Public frm As Form
  4.  
  5. Function OpenAClient()
  6.     'Open a new instance, show it, and set a caption.
  7.     Set frm = New Patient_IUR_Overview
  8.     frm.Visible = True
  9.     frm.Caption = frm.Hwnd & ", opened " & Now()
  10. End Function
  11.  
What am I missing?
Dec 2 '10 #10
ADezii
8,800 Expert 8TB
  1. Is Public frm As Form declared in a Public Code Module?
  2. Do you wish to create a Single Instance of the Form or Multiple Instances?
  3. If you cannot get this issue resolved, can you Upload the DB to either this Forum or my personal E-Mail Account?
Dec 2 '10 #11
sueb
379 256MB
Where do I find out whether the module it's in is public? And I do want to create multiple instances of the form; single instance has been working great all along--the whole point of this particular question was for my users to be able to have more than one instance of this form open at a time.
Dec 3 '10 #12
ADezii
8,800 Expert 8TB
It's not the Module that is Public, it is the Declaration of the frm Variable. Look at the latest Attachment.
Dec 3 '10 #13
sueb
379 256MB
In the code that I posted, is it not correct?
Dec 3 '10 #14
ADezii
8,800 Expert 8TB
Yes, as long as Public frm As Form is Declared in a Standard Code Module.
Dec 3 '10 #15
NeoPa
32,183 Expert Mod 16PB
I shall have to look at this sometime. I've heard Access is designed to support multiple form instances, but I've not used it myself yet.
Dec 5 '10 #16
ADezii
8,800 Expert 8TB
T^he Attachment in Post# 2 is a good, basic example.
Dec 5 '10 #17
NeoPa
32,183 Expert Mod 16PB
Thanks for that ADezii. I looked at the database and found there were a number of questions that were not answered directly, but Allen Browne's article (Managing Multiple Instances of a Form), where presumably this database came from originally in it's basic form, made things somewhat clearer.

Basically, the way to create a separate instance of a form rather than simply switching to the main instance, is to use code to create a new object, or instance, of that form's class (Set frm = New Form_frmClient), rather than the more usual call to DoCmd.OpenForm(). A side-effect of this is that once the last reference to this in the code is lost (removed from the collection in this case, but also if the collection or other variable where it is referenced were to go out of scope) then the item is automatically closed by Access.

This is why it is so important that frmMain links it's buttons to routines in a standard module rather than its own related module (although the only really important part to keep there is actually the instance collection. The code could be managed in its own module, albeit with certain complications).

As Allen Browne says, take great care to manage the references to the instances cleanly in your code. It would be very easy to lose instances in strange places if they are not correctly maintained. The example database gives a good example of how they can be maintained reliably.
Dec 5 '10 #18
NeoPa
32,183 Expert Mod 16PB
SueB:
In the code that I posted, is it not correct?
It appears to me that the instance is created pretty well, but it's not clear where you have stored the reference in order that it doesn't go out of scope almost immediately and so disappear before it's even seen. Can you explain what you have done in this respect?

See post #18 and the article linked therein, for why this is so important an aspect of the whole process.
Dec 5 '10 #19
ADezii
8,800 Expert 8TB
@NeoPa, I feel as though you brough up an excellant point in that it is very beneficial to maintain the Class in order to manage the Form Instances. In this particular case, however, the Op only wishes to create a Single Instance, and manageability is really not an issue as I see it.
Dec 5 '10 #20
NeoPa
32,183 Expert Mod 16PB
...and yet it appears to me (at least) that this is exactly where Sue is struggling at the moment (assuming I'm understanding her response correctly).

ADezii, also you shouldn't underestimate the impact that threads such as these have on other members and the public in general. I don't recall another that covered this subject - so this could prove particularly important - and to more than just Sue.
Dec 5 '10 #21
sueb
379 256MB
Well, I guess I'm just going to give up on this. I don't understand most of the discussion, and I don't know how to answer questions about "where" this code is, and I can't figure out how to make it more public than it is.

I realize how lame this is, but I think I'll just put another choice on the Switchboard that will have a second copy of the forms so that my users can at least get up one more patient when they need to.

Thanks anyway, everyone, for trying to help me with this.
Dec 10 '10 #22
ADezii
8,800 Expert 8TB
sueb, can you Upload your Database, so that we can see exactly what is going on? If the Data is sensitive, I can give you my Personal E-Mail Address in a Private Message. The decision is yours: either through in the towel or Upload the DB.
Dec 10 '10 #23
sueb
379 256MB
It would be great to have someone look at it for me! Tonight, when I can have the database to myself, I'll scrub it and then upload a version that isn't sensitive. Thanks!
Dec 10 '10 #24
ADezii
8,800 Expert 8TB
You are quite welcome, sueb. Sometimes you just get to a point where you need to look at the physical DB. Along with the Attachment, explain in detail the exact problem that you are encountering.
Dec 10 '10 #25
NeoPa
32,183 Expert Mod 16PB
See if this helps :

Basically, the creation of the form instance (copy of the form) must be done in code using code similar to :
Expand|Select|Wrap|Line Numbers
  1. Set frm = New Patient_IUR_Overview
You have this bit correct already.

The problem is that the variable frm gets trashed as soon as it goes out of scope, and whenever any object which is created in code, ends up with no references left to it (No object variables in code anywhere that point to it), it is automatically tidied away by Access.

It seems to me that frm, in your code, is defined within a Switchboard form. This is fine, but it does mean that as soon as this form is closed, the variable frm goes out of scope and, assuming nothing else is set to contain a reference to this newly opened form, this will cause that form to disappear immediately.

Standard modules, not the ones associated with any particular classes (like the form or report modules), have a scope of the whole session of a project (or until the project is reset, which happens when developing sometimes). This means that if you assign the new form to a variable defined in a standard module, it will never go out of scope while the project is active, which is ideal for what you need. Clearly it would need to be defined within that module as Public. FYI modules that are standard modules can be seen in the Project Explorer pane of the IDE (See Debugging in VBA) in the category called simply Modules. If there is none there then you can create one in the same pane of the IDE by right-clicking and selecting Insert / Module.

I hope this helps to put things into perspective. It seems you are so near to a correct solution, it would be a shame to fall so close to the finish line.

Best of luck though, however you prefer to play it :-)
Dec 11 '10 #26
sueb
379 256MB
Thanks for this explanation. I haven't had a chance to get the database ready for uploading, but let me try to put you in the picture.

When I open my database for working on it (I'm running 2003), a pane opens that lists on the left the various pieces of the database (Tables, Queries, etc.). When I select the "Modules" group, there is a module there (I didn't create it, so I'm assuming it's sort of a default) that starts with the declaration "Option Compare Database", and then has the code for "Sub RecoverCorruptDB()". I assumed that this was a public module, so following the end of that routine, I placed the code I listed in post #10.

Is the problem that I need to put this in a separate module or something? That seems a bit weird. I actually have never come across a diagram outlining the relationships among the modules that would be listed in this pane, or how they interact with the rest of the objects, and I haven't found a good reference that just tells a developer (who knows how to do all this in other applications) what maps to what in Access/VBA.

I hate to sound so lazy about this, but I'm given so little time to work on this that I'm willing to cut any number of corners just to get to my goal. I realize this means that my understanding (at least for the time being) will be spotty, and will limit my ability to develop independently, but I can't see any way around that at present. I apologize to the community for being such a leech.

Any enlightenment you can give me would be most appreciated.
Dec 13 '10 #27
ADezii
8,800 Expert 8TB
From what you have said, it appears that it should work butu I'll still need to see the DB.
Dec 13 '10 #28
sueb
379 256MB
I'll try to get this uploaded as soon as possible. It looks like I won't be able to work on it today, so maybe tomorrow. Again, thanks for being willing to examine it for me.
Dec 13 '10 #29
sueb
379 256MB
I sneaked in a few minutes, so here is a copy of my database and front end. Let me know if I forgot anything. Thanks again!
Attached Files
File Type: zip upload.zip (2.78 MB, 153 views)
Dec 13 '10 #30
ADezii
8,800 Expert 8TB
sueb, check the Revisions and see if this is what you are looking for. I am sending (Attachment) you only the Front End since that is where the changes were made, and you will also need to ReLink the Back End Tables since I had to change the Path for me to work on the DB.
Attached Files
File Type: zip IURs for upload.zip (220.2 KB, 116 views)
Dec 13 '10 #31
NeoPa
32,183 Expert Mod 16PB
ADezii, I'm interested in keeping up with the thread. What problems did you find and what fixes did you make to Sue's database?
Dec 14 '10 #32
ADezii
8,800 Expert 8TB
The main problem, NeoPa, appears to have been my stupidity, to which I owe sueb my sincere apologies since I know that she went crazy trying to figure it out and was just about ready to quit. The Demo that I presented in Post# 9 was correct and fully functional. The code that I posted in Post# 10 was incorrect in that:
Expand|Select|Wrap|Line Numbers
  1. Set frm = New Patient_IUR_Overview
should have been:
Expand|Select|Wrap|Line Numbers
  1. Set frm = New Form_Patient_IUR_Overview 
with the Form_ qualifier.

The code now should create a Single Instance of the Patient_IUR_Overview Form as sueb requested. In order to create Multiple Instances, it appears as though you would need to create a Class Structure to contain them.

P.S. - @sueb, sorry again for the oversight.
Dec 14 '10 #33
sueb
379 256MB
Okay, then! Thanks for the update! (I just modified that line in my code instead of copying your version and re-linking the back end.)

So, now, I need to know how VBA handles class structures. I'm very familiar with class structures in Ada, so I'm assuming that, once I understand how that maps to VBA, I'll be off and running.
Dec 14 '10 #34
sueb
379 256MB
What other changes did you make? I ask because after modifying my code, it still behaves just as it did before, only giving me that flash and no patient window.
Dec 14 '10 #35
ADezii
8,800 Expert 8TB
In the Standard Code Module (Module 1):
Expand|Select|Wrap|Line Numbers
  1. Public frm As Access.From
P.S. - The Attachment in Post# 2 will show you how to use a Class to manage Multiple Form Instances. When I have time, I will also revise your Front End to include this functionality and Resend to you.
Dec 14 '10 #36
ADezii
8,800 Expert 8TB
sueb, I've revised your Front End once again in order to demonstrate how you can create Multiple Instances of a Form (Patient_IUR_Overview) via a Switchboard, while using a Collection to manage them. You can now Open as many Instances of Patient_IUR_Overview as you like directly from the Switchboard, Close each Instance individually, or Close ALL Instances via the Close ALL Clients Option on the Switchboard. Pay close attention to:
  1. The Close() Event of the Patient_IUR_Overview Form.
  2. The Close ALL Clients Option on the Switchboard.
  3. The OpenAClient() Function.
  4. The CloseAllClients() Function.
  5. The Collection Declaration in the General Declarations Section of Module1.
Attached Files
File Type: zip IURs for upload_2.zip (230.6 KB, 193 views)
Dec 14 '10 #37
sueb
379 256MB
Oh, wow, you are a genius or at least an angel, ADezii! This is wonderful! I really can't thank you enough for taking such time and trouble with this for me. Thanks, my friend.
Dec 15 '10 #38
ADezii
8,800 Expert 8TB
The pleasure was all mine, sueb, and again I apologize for my Typo which almost forced you give up on this Project.
Dec 15 '10 #39
sueb
379 256MB
Oh my gosh, you shouldn't be apologizing for what was essentially merely impatient despair on my part. You put in such effort for me, and I'm so grateful. (This forum makes me look great at work, I'll tell you that right now!)
Dec 15 '10 #40
NeoPa
32,183 Expert Mod 16PB
ADezii is not only very helpful, often going to great lengths to help someone resolve an issue, he's also very modest and feels responsible whenever anything he does falls any way short of perfect. Two of more reasons why we love him here :-)

ADezii:
In order to create Multiple Instances, it appears as though you would need to create a Class Structure to contain them.
I'm not sure a class structure, specifically, is necessary. Certainly it could be implemented that way, but the link I referred to earlier (post #18) from our friend Allen Browne, illustrates how this can be done using a public collection of form objects defined in a standard module instead.

PS. Congratulations from me for seeing Sue through to a solution she can work with.
Dec 15 '10 #41
ADezii
8,800 Expert 8TB
@NeoPa, thanks, although I did come very close from derailing her on this Project. I must have looked at the Code 100 times before it finally hit me like a ton of bricks! (LOL).
Dec 15 '10 #42
NeoPa
32,183 Expert Mod 16PB
A perfectly understandable mistake ADezii. Made more likely by the fact that we are all, here, working on databases we generally don't have proper access to. Working blind in most cases. You should cut yourself a little slack sometimes maybe. There are not many who would even be capable of working at that level. Cast your mind back to when you started. I expect you'll remember, as I do, how difficult it was to get working that way in so many situations (Not all, but many certainly).
Dec 15 '10 #43

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Markus Prediger | last post: by
11 posts views Thread by Clark Stevens | last post: by
1 post views Thread by =?Utf-8?B?UGFkYW0=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.