dbTalk Databases Forums  

Create a new Form instance by name using generic code

comp.databases.ms-access comp.databases.ms-access


Discuss Create a new Form instance by name using generic code in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Wilhemina
 
Posts: n/a

Default Create a new Form instance by name using generic code - 12-08-2011 , 04:24 AM






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

Reply With Quote
  #2  
Old   
Ron Paii
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-08-2011 , 10:11 AM






"Wilhemina" <cleitean-cleit8 (AT) yahoo (DOT) co.uk> wrote

Quote:
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
If you are opening the form for user input it is as simple as this.

DoCmd.OpenForm strFormName

Reply With Quote
  #3  
Old   
Albert D. Kallal
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-08-2011 , 04:42 PM



"Wilhemina" wrote in message
news:beb3a098-94c5-4d3e-afdd-47d6fa71c48f (AT) o1g2000vbe (DOT) googlegroups.com...


Quote:
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 form to
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_kallal (AT) msn (DOT) com

Reply With Quote
  #4  
Old   
Albert D. Kallal
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-08-2011 , 04:53 PM



"Ron Paii" wrote in message news:jbqnkf$4a8$1 (AT) dont-email (DOT) me...


Quote:
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

Reply With Quote
  #5  
Old   
Tony Toews
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-08-2011 , 07:30 PM



On Thu, 8 Dec 2011 15:42:57 -0700, "Albert D. Kallal"
<PleaseNOOOsPAMmkallal (AT) msn (DOT) com> wrote:


Quote:
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've never seen any such either. But then this isn't a question that
comes up often.

Note that one quirk is if the form has a space in the name. You must
then use an underscore in the VBA code.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #6  
Old   
Tony Toews
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-08-2011 , 07:30 PM



On Thu, 8 Dec 2011 02:24:47 -0800 (PST), Wilhemina
<cleitean-cleit8 (AT) yahoo (DOT) co.uk> wrote:

Quote:
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)
But that wouldn't work in an MDE because you can't create VBA code in
an MDE.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #7  
Old   
Wilhemina
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-09-2011 , 03:57 AM



Thanks for the replies. Albert, you're exactly right about why I want
to have multiple instances running.

I experimented with the wrapper form idea and got a system that works
transparently for simple cases. But if i want to extend it to cope,
say, with displaying forms as popups in windows of a certain size,
with dynamic resizing of certain display elements of the form, it all
becomes rather upsetting! If I was designing a clean system from the
start with this in mind, it might be a going concern, but trying to
retrofit existing code while maintaining compatibility isn't going to
produce anything pretty.

I think your hard-coding idea is the best solution - unfortunately not
entirely reusable in different projects, but the overhead required to
customise it for each project is relatively low.

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!

Thanks again
Wilhemina


On Dec 8, 10:42*pm, "Albert D. Kallal" <PleaseNOOOsPAMmkal... (AT) msn (DOT) com>
wrote:
Quote:
"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

Reply With Quote
  #8  
Old   
Ron Paii
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-09-2011 , 07:17 AM



"Albert D. Kallal" <PleaseNOOOsPAMmkallal (AT) msn (DOT) com> wrote

Quote:
"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
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.

Reply With Quote
  #9  
Old   
Tony Toews
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-10-2011 , 01:11 AM



On Fri, 9 Dec 2011 01:57:41 -0800 (PST), Wilhemina
<cleitean-cleit8 (AT) yahoo (DOT) co.uk> wrote:

Quote:
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!
Hehehe. Agreed that it's a hack too far. Excellent way of putting
it.

Indeed I'd go so far as put in a check to see if this is an MDE. And
if so pop up a message indicating that this is a problem and why.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #10  
Old   
Albert D. Kallal
 
Posts: n/a

Default Re: Create a new Form instance by name using generic code - 12-10-2011 , 06:54 PM



"Ron Paii" wrote in message news:jbt1ph$v82$1 (AT) dont-email (DOT) me...

Quote:
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.
You can most certainly change the record soure of a form.

eg:

Dim f1 As New Form_testform
Dim f2 As New Form_testform
Dim f3 As New Form_testform

f1.RecordSource = "select * from customers1"
f1.Visible = True

f2.RecordSource = "select * from customers2"
f2.Visible = True

On the other hand, as above shows, needing multiple tables to be used with
the SAME format suggests a normalizing problem. Perhaps just adding a single
column to the table that distinguishes each table would mean then you just
filter the form by that new column, not have to work with and maintain
multiple copies of the same table.

Anyway, regardless of the normalizing issue, you can simply set/change the
record source of any form by setting the RecordSource property as above.

You can even in the form code go:

me.RecodSource = "select * from sometable where City = 'abc'"


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
Pleasenospam_kallal (AT) msn (DOT) com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.