![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi I'm trying to produce a procedure that will create a new instance of a form whose name is passed as a string to the proc. I know I can do it this like Set frm = New Form_MyForm but what I want to do is have generic code that will work for any form - something along the lines of Set frm = new "MyForm" I've trawled the groups and found several people asking the same thing, but with no answer. I can think of a couple of hacks that would work. The nastier of the two would be to have code that created a 'disposable' procedure, on demand, containing Set frm = New Form_MyForm (where MyForm would be the name of whatever form I wanted to instantiate). This could then be run and the reference to the new instance of the form returned (and the procedure then destroyed or kept and overwritten the next time) The alternative would be to have a 'wrapper' form that contained only a subform control in which it loaded the form I wanted to instantiate. In this case I could hard-code the form name since it would always by the same: set frm= new Form_WrapperForm frm!SubForm.SourceObject="MyForm" Is there a better way to do this? Thanks Wilhemina |
#3
| |||
| |||
|
|
The alternative would be to have a 'wrapper' form that contained only a subform control in which it loaded the form I wanted to instantiate. In this case I could hard-code the form name since it would always by the same: set frm= new Form_WrapperForm frm!SubForm.SourceObject="MyForm" Is there a better way to do this? |
#4
| |||
| |||
|
|
If you are opening the form for user input it is as simple as this. DoCmd.OpenForm strFormName |
#5
| |||
| |||
|
|
The alternative would be to have a 'wrapper' form that contained only a subform control in which it loaded the form I wanted to instantiate. In this case I could hard-code the form name since it would always by the same: set frm= new Form_WrapperForm frm!SubForm.SourceObject="MyForm" Is there a better way to do this? Golly, I don't think so. Your sub form idea is almost close here. |
#6
| |||
| |||
|
|
I can think of a couple of hacks that would work. The nastier of the two would be to have code that created a 'disposable' procedure, on demand, containing Set frm = New Form_MyForm (where MyForm would be the name of whatever form I wanted to instantiate). This could then be run and the reference to the new instance of the form returned (and the procedure then destroyed or kept and overwritten the next time) |
#7
| |||
| |||
|
|
"Wilhemina" *wrote in message news:beb3a098-94c5-4d3e-afdd-47d6fa71c48f (AT) o1g2000vbe (DOT) googlegroups.com... The alternative would be to have a 'wrapper' form that contained only a subform control in which it loaded the form I wanted to instantiate. In this case I could hard-code the form name since it would always by the same: set frm= new Form_WrapperForm frm!SubForm.SourceObject="MyForm" Is there a better way to do this? Golly, I don't think so. Your sub form idea is almost close here. I would have hopped something like this: Dim f * * * As Form Set f = db.Containers("forms").Documents("myForm") However, containers returns a document type object, not form. The best best is to create a routine with case statements, and for each form you add, then you add a case statement. eg: Public Function GetFormInstance(strForm as string) as Form select case strForm * *case "frmCustomers" * * * set GetFormInstance = form_frmCustomers etc. If you do cook up something better, I would love a solution for this. I have a number of application where I allow multiple instances of a formto be opened at the same time. I mean, even allowing a user to minimize the current form while on a phone, and launch another copy of the same form can often be rather useful. I looked for a solution to your question, but decided that simply hard coding some case or if/then was the best I could find. -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada Pleasenospam_kal... (AT) msn (DOT) com |
#8
| |||
| |||
|
|
"Ron Paii" wrote in message news:jbqnkf$4a8$1 (AT) dont-email (DOT) me... If you are opening the form for user input it is as simple as this. DoCmd.OpenForm strFormName Unfortunately such an approach does not allow you to have multiple instances, or more than one copy the form opened at the same time. Keep in mind that such designs are possible in access, and this is what the posters asking for. Is application approach can be very handy, since you can have a complex form where persons halfway through entry and data into the form, the phone rings, and they have to look up another customer, but use the same complex data entry form that there currently end, when you allow multiple instances of the same form, the user can minimize the current form they are on, and launch another new separate copy of exactly the same form. They can have several easy to open, and when the dow with each customer they simply close each one, and eventually return back to the original form that they were working on. It is a great and fantastic ability of access, but unfortunately as the poster points out you have to hard code in VBA the instance of the form that you're going to recreate, and the posters looking for solution in which you can just pass a string or variable name of the form that you want to open more than one copy of. As noted, I don't think this is possible, and I wish it was. The compromise solution I suggest of using a set of "case statements" and public function to return the form instance should suffice. As noted, you can't launch multiple instances of the same form by using the OpenForm command, you have to use VBA and create a variable instance of the form object in memory, and then set the visible property = true. create a VBA module, and try this code: Dim f1 As New Form_testform Dim f2 As New Form_testform Dim f3 As New Form_testform f1.Visible = True f2.Visible = True f3.Visible = True DoEvents MsgBox "wait" Replace "testform" in above with the name of a form you have. Run the code. you will see 3 copies of the SAME form. When you hit ok, then all 3 forms will close since they all go out of variable scope. If you declare f1,f2,f3 as global vars they would NOT go out of scope and after you run above you will have 3 copies of the same form loaded. It is fun and cool! It is a slick trick and one I used before. However, the poster is asking can we eliminate the hard coding of the form name like I did in above - I don't think we can and I wish we could. I also believe that the above only works for forms with code modules. Anyway, if anyone has a solution - I would love to be shown I am incorrect in public and would love this kind of egg on my face! -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada Pleasenospam_kallal (AT) msn (DOT) com |
#9
| |||
| |||
|
|
Tony, I take your point about the disposable code option and mde files - I hadn't thought about that because I rarely use them. The idea of writing and destroying procedures on the fly also just strikes me as a hack too far! |
#10
| |||
| |||
|
|
That is cool! OT: I have a form with a private temp table record source that would be useful to open more then once. Would I need multiple tables and change the record source for the 2 and 3rd instance of the form? I once saw a sample application using class modules but I can't find it. |
![]() |
| Thread Tools | |
| Display Modes | |
| |