[AccessD] RE: Instantiate Forms as Objects

David Beckles becklesd at tiscali.co.uk
Wed Jan 21 19:10:09 CST 2004


Dear Ken,
One way of achieving (most of) what you want to do is as follows:

1. In each of your forms create the function
Public Function NewInstance() As Form
     Set NewInstance = New Form_TestForm
End Function

Its purpose is merely to return a reference to a new instance of the form. 
You could also define it as a new public property of the form, instead of a 
function.

2. Define the following function in some general module

Public Function OpenFormMultiple(FormName As String) As Form
     ' check the Forms collection, and if the form is in it then use the 
NewInstance() method of the form
     ' otherwise open the form in the usual way and return a reference to it.
     ' in either case, the form will not be visible.
     ' if any errors occur, then the value Nothing will be returned, so 
remember to test for it.

     On Error Resume Next        ' You could use fancier error handling if 
you wanted
     Dim F As Form

     Set OpenFormMultiple = Nothing
     Set F = Forms(FormName)
     If F Is Nothing Then
         Application.Echo False  ' this is to suppress the screen flicker
         DoCmd.OpenForm FormName
         Set F = Forms(FormName)
         F.Visible = False
         Set OpenFormMultiple = F
         Application.Echo True
     Else
         Set OpenFormMultiple = F.NewInstance()
     End If
End Function

One problem with this arrangement is that you can close all the instances 
of a form except the first by setting the reference to Nothing. The first 
instance has to be closed by going into the Forms collection. I have not 
found a way around that as yet.

I hope that this helps,
David




More information about the AccessD mailing list