form instances

RangerTen

Rangers Lead The Way!
Local time
Today, 11:55
Joined
Jun 30, 2002
Messages
42
I want to use create a variable that will hold a new instance of a form when it is opened....if that makes sense...

Dim test As New Form_frmForm1

DoCmd.OpenForm "test"

doesn't work. how do I then open that form once the variable has been initialized. Don't know where to go from here.

Thanks.

Of course, what kind of problems will this cause if I have two instances of the same form open and they are both connected to the same table. Would this be similar to being logged into a database by multiple users?
 
Last edited:
Code:
    Dim test As New Form_frmForm1
    DoCmd.OpenForm test.Name

'but what's wrong with:

    DoCmd.OpenForm "frmForm1"

but I don't think you can open the same form twice simultaniously:confused:
 
Last edited:
I believe you can...that's one of the big things thats "changed" with vb.net. There is no default form instance. Just don't know the syntax in VBA
 
I know it's possible in .Net but Access 2k and 2k2 are not .Net.

Well if someone know's it, I'd love to see it in VBA.

Office 2k3 is set to release in November I think, and it will be .Net 2003 equivilant. While Office 2k and 2k2 are VB6 library/syntax equivilant. Office 97 is VB5.
 
Dim x As Form
Set x = New Form_frmForm1
will allow you to work on separate records at the same time
If you want to replicate more than one user open more than one copy of the db on the same pc with the same forms open
 
But how do you manipulate x then

docmd.openform "x" does not work

what do i do to work with each instance of the form.

I don't want to replicate multiple users, just work with multiple records from the same table
 
Last edited:
Some Button_Click()
Set x = New Form_frmForm1
x.SetFocus
End Sub


Dim x As Form belongs in the declarations section
 
THis does not allow multiple instances of the same form though. Every time I click the button, the form that was open disappears and a new one opens in the center of the screen. What is happening here? It may open a new instance, but the old instance closes...
 
Thanks...going to try with a clean blank database and see if that fixes my problem.
 
Strange thing...if I put this code in a form module (the one I want instantiated, it works fine). If I put the code as a public sub in a stand alone module, it closes the form I am trying to instantiate before it opens the form again. Strange.
 
Pono1,
Thanks for the links, it was a great lead.

RangerTen,
The KB Article explains it all, your "Strange Thing..."
When you open multiple instances of a form, the original instance is the only form object that is stored permanently in the database. The other instances are temporary and are removed from memory when you close them.
Because you keep calling the function from the original memory space, you are reseting (closing) the form that was opened recursively in the previous call before reopening a new one. The code works basically on recursion, if from the 1st instance of FORM1 you open a 2nd instance of FORM1, and then from the 1st instance you attempt to open a 3rd instance, you are essentally reseting the memory space of the 2nd instance, forcing it to unload so the 3rd instance can be loaded and essentially becoming the 2nd instance again.

I think this is the model that you want, but it doesn't work this way
Code:
1st instance (Form1)
 |__2nd instance
 |__3rd instance
 |__4th instance
 |__5th instance
As well as executing the call to open Form1 from a public module, you will reset the memory of the 1st loaded instance of Form1 everytime you execute it and never have more than one instance open.

Now, if you attempted to open the 3rd instance from the 2nd instance (NOT the 1st) you will be successfull, and from the 3rd you can open the 4th, and the 4th you can open the 5th.
Now that you have 5 Form1's loaded recursively, if you go back to the 3rd and attempt to open a 6th instance you will again reset the recursive memory of the 4th and 5th, forcing them to close so a NEW 4th can be loaded into memery.

a simple model of how it works
Code:
1st Instance (Form1)
  |__2nd instance
      |__3rd instance
          |__4th instance
              |__5th instance

I hope this is clear but I suggest you go back and reread the KB article again.

As you stated earlier, what you want to do can be done in .NET, but like I said earlier, Access 2k and 2k2 are not .NET.

good luck,
 
Last edited:
well, thank you...you're description was 100 % clearer to me than the Microsoft article. I read about occupying the memory space, but didn't understand it until you just ecplained it. Thanks...Makes much more sense...Guess I'll try to learn dotnet, because that is what I need to do.

Truly, thank you for all the time on this. You saved me hours upon hours of banging my head over this.
 
RangerTen,
I got thinking about how to get what your looking for, try the following.

Insert the following code into a public module
Code:
Dim frmTest(99) As Form
Global blnInstance(99) As Boolean
Global gblInstance As Integer

Public Function OpenTestForm() As Long
    GetInstance
    Set frmTest(gblInstance) = New Form_Form1
    frmTest(gblInstance).SetFocus
End Function

Public Function GetInstance() As Long
On Error Resume Next
    Dim i As Integer
    For i = 1 To 99
        If blnInstance(i) = False Then
            gblInstance = i
            Exit Function
        End If
    Next i
End Function
Then insert the following into the form that you want multiple instances of:
Code:
Public Instance As Integer

Private Sub cmdOpenForm_Click()
    OpenTestForm
End Sub

Private Sub Form_Load()
    Me.Caption = "Form Instance: " & gblInstance
    Me.Instance = gblInstance
    blnInstance(Me.Instance) = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    blnInstance(Me.Instance) = False
End Sub
Make sure to set the On Load, On Unload form properties to [Event Proceedures] and create a command button called cmdOpenForm and set it On Click property to [Event Proceedure] and test it.

It will allow you to open the form you want a maximum of 99 times regardless of which form instance you called it from, the problem is the 99 limit you could increase the limit but you will use/reserve more memory.
In order to keep track of what instances are available I created the blnInstance(99) global array and when the forms load and unload they set the value to TRUE and FALSE so when the GetInstance function is called it looks for the first glbInstance(i) that is false so it knows that instance is available for utilizing in memory.
It's kind of quick and crude, but it seems to work well in an Access 2000 project.

Try it out and let me know if you have any problems, I'm thinking it's kind of cool, and already know a couple of places where I could use it and I'm getting all kinds of idea's on how else to use the same idea.
 
Last edited:
Minor Snag...

There is a minor snag when closing an instance, if you use the following:
Code:
    DoCmd.Close acForm, Me.Name
since all the instances have the same form name the above will by default close the first instance of the form in memory even though you wanted to close the 3rd instance the original 0 (zero) instance will be the one to close.
There will be other problems with referenceing one specifically by name for other events as well.

use the following to close a form instance
Code:
    DoCmd.Close acDefault

If I figure out more on how to reference a specific instance I'll post it here.
 
EXCELLENT. Thanks.... See, I told you it was possible :D

Thanks for all the help.
 
A late addition to this thread

The preceding has been most instructive.

I, too need to call a form recursively. But I need to wait until the child instance closes and returns a value - probably by putting an entry into a global array (like the blnInstance arrays mentioned already) unless there are some more sensible suggestions, before resuming execution in the parent form - as follows

* instance 1 of form_x
* call instance 2 of form_x and wait for it to close
* call instance 3 of form_x and wait for it to close
* call instance 4 of form_x and wait for it to close
* instance 4 closes, returning a value in a global array
* use values returned from instance 4 of form_x
* instance 3 closes, returning a value in a global array
* use values returned from instance 3 of form_x
* instance 2 closes, returning a value via a global array
* use vaules returned from instance 2 of form_x
* instance 2 closes, returning a value in a global array

If I didn't need to call the form recursively, I could use

DoCmd.OpenForm "form_x", , , , , acDialog
 
If you really have to ;) then have a look at Alllen Brownes article http://allenbrowne.com/ser-35.html, which has another way of referring to different instances.

I've never worked with this, so I can't offer more assistance, but there should be some samples around that should be found through a web search
 
If you really have to ;) then have a look at Alllen Brownes article http://allenbrowne.com/ser-35.html, which has another way of referring to different instances.

I've never worked with this, so I can't offer more assistance, but there should be some samples around that should be found through a web search

Allen's code is the same approach that is outlined in the Access Developers Handbook. I've used variations on that approach in many apps. The problem it resolves is managing the different instances, which is pretty important.
 

Users who are viewing this thread

Back
Top Bottom