More than one copy of a form open at the same time

  • Thread starter Thread starter Ryan Cauchi-Mills
  • Start date Start date
R

Ryan Cauchi-Mills

How can you get Access to open more than one copy of a form at the same
time. I would like the ability to open more than 1 job form at a time.
Maybe I've missed something....
 
HI,
yes, it's simple, see
dim frm01 as Form_myForm01 ( <-- this is the real form name)
set frm01 = new Form_myForm01
frm01.visible=true
.... do all need with it.
THE only one problem - this form is living untile you or VB destroy veriable
frm01 or set frm01=nothing

BSF
Moscow
 
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
 
Boy, that seems a bit complex! Far easier, surely, to keep the form
references in an array, and have the calling form stay around until the
called forms are no longer required.

Not much more than:

nforms = nforms + 1
fform (nforms) = new form_blah

Cheers,
TC
 
I thought you might reply with some benefits of the more-complex method.

I gather there aren't any.

TC
 
<<I thought you might reply with some benefits of the more-complex method.>>
No you didn't! What you said was:
<<Boy, that seems a bit complex! Far easier...>>

Having seen several of your posts, you seem like someone who knows a bit
about Access. That being the case, I figured you would immediately see the
benefits. I'm surprised to see that you didn't (on either occasion).

How would you suggest the instantiated forms communicate with the calling
form without violating OO re-use?
* How would the calling form know when to destroy the reference to an
instantiated form?
* How would it know *which* form instance to destroy?

I would be very interested to learn how you can extrapolate that
functionality using:
nforms = nforms + 1
fform (nforms) = new form_blah

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
 
I can't help by feel responsible for the ill feeling here. HAHAHAHA.
GRS: Cheers for the lesson, you've taught me more than I was looking for -
Great!
TC: *grin*
 
Back
Top