Ryan,
Instantiating a form IS as simple as BSF suggests, however, if you want to
do anything meaningful with multiple forms, you need to track them, and more
importantly, keep them instantiated until YOU decide to destroy them. To do
that is not quite so simple.
'************************
'Place the following code into a standard module.
'************************
Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As Any) As Long
Public Function CreateGUID() As String
Dim bByte(0 To 15) As Byte
Dim iCtr As Long
Dim sTemp As String
On Error GoTo Proc_Err
CreateGUID = ""
If CoCreateGuid(bByte(0)) = 0 Then
For iCtr = 0 To 15
sTemp = sTemp & IIf(bByte(iCtr) < 16, "0", "") &
Hex(bByte(iCtr))
Next iCtr
Else
MsgBox "Error while creating GUID!"
End If
CreateGUID = sTemp
Proc_Exit:
Exit Function
Proc_Err:
DoCmd.Beep
MsgBox "Unknown error." & vbCrLf & vbCrLf & "Failed to create GUID.", _
vbOKOnly + vbExclamation, "Could not create GUID"
Resume Proc_Exit
End Function
'************************
'Now create two forms; one called frmForm1,
'and the other called frmForm2, and place a
'command button on each (both named Command0).
'On frmForm1, 'add a textbox called Text1.
'************************
'Now add the following code to frmForm1
'************************
Private frm As Form_frmForm2 'The form to multi-instantiate
Private colForm2 As Collection 'The form collection
Private Sub Command0_Click()
'Instantiate the new form instance
Set frm = New Form_frmForm2
'Make it visible
frm.Visible = True
'Set a circular reference to frmForm1
Set frm.ParentForm = Me
'Destroy the local reference
Set frm = Nothing
End Sub
Public Sub NotifyFormOpen(GUID As String)
'This sub is called from frmForm2's Close event
'Add the newly created form to the forms collection
colForm2.Add frm, GUID
Refresh the counter to show how many we current have
Me.Text1 = colForm2.Count
End Sub
Public Sub NotifyFormClose(GUID As String)
'This sub is called from frmForm2's Open event
On Error Resume Next
'Release the form's circular reference to frmForm1
Set colForm2(GUID).ParentForm = Nothing
'Remove the form from the collection
colForm2.Remove GUID
'Refresh the counter
Me.Text1 = colForm2.Count
End Sub
Private Sub Form_Load()
'Instantiate the form collection
Set colForm2 = New Collection
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
'Release the form reference
Set frm = Nothing
'Release the Collection object and all the objects it contains
Set colForm2 = Nothing
End Sub
'************************
'Now add the following code to frmForm2
'************************
Private bIsObject As Boolean 'True if the form has been instantiated
remotely
Private sGUID As String 'The form's unique GUID (identifier)
Private oParent As Form 'An object variable to contain a
'circular reference to the calling form
Private Sub Command0_Click()
'Cose the current form instance
DoCmd.Close
End Sub
Public Sub Form_Unload(Cancel As Integer)
On Error Resume Next
'Call the parent form's NotifyFormClose event
If bIsObject Then oParent.NotifyFormClose sGUID
'Release the local object reference
Set oParent = Nothing
End Sub
Public Property Get GUID() As String
'Set the GUID
GUID = sGUID
End Property
Public Property Set ParentForm(fm As Form)
'Instantiate the local circular reference to the calling form
Set oParent = fm
If Not bIsObject Then
'Get the GUID and store it locally
sGUID = GetGUID
'The first time this property is called,
'call the parent form's NotifyFormOpen event
oParent.NotifyFormOpen sGUID
End If
'Set the flag to indicate that the form has been
'instantiated remotely
bIsObject = True
End Property
'************************
'Whenever you want to do anything with one of the instances of frmForm2,
'you can refer to it through the Collection object, like so:
' colForm2(sGUID).public_method_or_object
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html