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
  #11  
Old   
Ron Paii
 
Posts: n/a

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






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

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

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.

It's a temp table for a data entry form. Each user has a copy of it in a
temp.mdb file stored alongside their copy of the FE.

Reply With Quote
  #12  
Old   
Jon Lewis
 
Posts: n/a

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






As others have said you do seem to have to use the literal object name with
the New keyword.
What you can do to make the procedure 'semi' generic is to maintain a
Collection of the instances of your form as you open them (including the
first instance). This allows you to use the same (and thus generic) form
variable instead of frm1, frm2 etc. for each instance. Also you can do
stuff to a particular instance (such as resizing as you mention) by using
the collection item. e.g.

Option Compare Database
Option Explicit
Dim clnFrm As New Collection
Dim frm As Access.Form

Private Sub Command1_Click()
Set frm = New Form_Form2
clnFrm.Add frm
frm.Visible = True
'Make subsequent instances Modal
If clnFrm.Count > 1 Then
clnFrm.Item(clnFrm.Count).Modal = True
End If
clnFrm.Item(clnFrm.Count).SetFocus
End Sub


You would probably need to add a Key to each item as you add it to the
collection so you can refer back to a specific form instance e.g. to remove
the item from the collection when the corresponding form instance is closed.

HTH

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

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 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_kal... (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.