Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Instances of form 1

Status
Not open for further replies.

rfoye

Programmer
Oct 15, 2004
40
0
0
US
I have code that creates identical instances of a form by adding to the collection for the form.

I would like to create multiple instances, HOWEVER, each instance would need to parse an OpenArgs value to set a recordset for the instance. Is this possible?

In other words (I think), is it possible to use the DoCmd.OpenForm command on a member of Forms_frmTest ?

-------------------
Rob Foye
Database Management
Regions Bank
 
For our multiple form instances, we expose additional properties, operations, and collections to pass data to the form instance.

Because of this functionality, we never use the OpenArgs parameter, even for single instance forms.

Hope that helps,

Gary
gwinn7
 
There is a couple of ways to do it. One way would be to use a global variable that you set before the form is instantiated. Ex:


public strRecordSourceName as string

set the record source name before instantiating the form instance

in the forms on load event you can just do

me.recordsource = strRecordSourceName

You could actually set the rs instead of the recordsource

me.recordset = glblRecordset

Now, set the global variable and open another instance.
 
Rfoye, just a question, why so many instances of the same form. I've seen this before and I can assure you that depending on your circumstances, you may be barking up the wrong tree. I actually tried to do this myself not too long ago and it turned out to be a disaster. Multiple forms as handy as they are can be very taxing, and so I would caution you against it. Let me know what you are trying to accomplish and I will let you know what worked for me. As I said, I made this same mistake once.

But to anwser your question, try something like this

DoCmd.OpenForm "FormName",,,"whereitemfromtable =" & Me.myfield,acnormal

Let me know if this helps
 
I disagree with the nwprogs post. You can not open multiple form instances with
DoCmd.OpenForm "FormName",,,"whereitemfromtable =" & Me.myfield,acnormal

Each form instance shares the same name property so openform does not work. If you need to manage multiple form instances you may want to create your own custom collection. But here is what I was thinking (all untested)
Code:
dim frmOne as Forms_frmTest
dim frmTwo as Forms_frmTest
dim frmThree as Forms_frmTest

'Open 1
strRecordSource = "qryOne"
set frmOne = new Forms_frmTest
frmOne.visible = true

'Open 2
strRecordSource = "qryTwo"
set frmTwo = new Forms_frmTest
frmTwo.visible = true

'Open 3
strRecordSource = "select * from tblTest where myfield = True"
set frmThree = new Forms_frmTest
frmThree.visible = true

Another way without the global variable is to simply open the form instances and then set the recordsource

Code:
dim frmOne as New Forms_frmTest
dim frmTwo as New Forms_frmTest
dim frmThree as New Forms_frmTest

frmOne.visible = true
frmTwo.visible = true
frmThree.visible = true


frmOne.recordsource = "qryOne"
frmTwo.recordsource = "qryTwo"
frmThree.recordsource = "qryThree"

frmOne.requery
frmTwo.requery
frmThree.requery
 
Thanks, MajP. I think your suggestion will start me on the path to find a solution, if there is one.

Unfortunately, I'm under a time constraint and can't play around with this idea anymore. Instead, I'm having to continue using 2 virtually identical forms. But when I work on "version 2", I'll keep this in mind.

-------------------
Rob Foye
Database Management
Regions Bank
 
You should really avoid global variables whenever possible. My advice is to design a class to manage your multiple instance forms. Example...

(In your Class)
private myforms as collection
private icount as integer

public function GetForm(i as integer) as form
on error resume next
set getform = myforms(i)
end function

public function OpenForm(mysql as string) as integer
if myforms is nothing then set myforms = new collection
' put code here to create your instance
myforms.add newform, icount
icount = icount + 1
' etc...
end function

Obviously there is more code to write and test, but I think you can get the idea. In this case the only Global Variable(if you think it necessary) would be a reference to the instantiated Class used for managing your duplicate forms.

Like I said in my previous post, we use this technique in our production app with no problems.

Gary
gwinn7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top