Form Instance and Objects

G

Guest

I am having a variable scope problem with mulitple instances of the same
form. I have the following code to create a new instance of a form and add it
to a global collection of forms:

--------------------
Set frmToOpen = New Form_frmObjectMain 'Create instance
mcGen.OpenGlobalForm frmToOpen, RootObject 'Global routine to add to
Collection
--------------------

The problem is with the 'RootObject' variable above.

Routine that adds the form to the global collection

--------------------
Public Function OpenGlobalForm(OpenForm As Form, ByVal Source As Object) As
Boolean
On Error GoTo ErrHandler
Dim strX As String

Select Case TypeName(Source)
Case Is = "clsContacts"
strX = Source.ContactName
Case Else
End Select

OpenForm.Caption = strX & OpenForm.Caption & " - Information"
Set OpenForm.SourceRootObject = Source
Set OpenForm.FormDisplayedBy = Forms.Item("frmMain")

On Error GoTo CollectionError
If gcolForms Is Nothing Then Set gcolForms = New Collection
gcolForms.Add OpenForm, OpenForm.Caption

With gcolForms.Item(OpenForm.Caption)
DoCmd.MoveSize (gcolForms.Count - 1) * 400, (gcolForms.Count - 1) *
400
.Visible = True
End With

Set OpenForm = Nothing
On Error GoTo 0
Exit Function

CollectionError:
If Err.Number = 457 Then
gcolForms.Item(OpenForm.Caption).SetFocus
Else
GoTo ErrHandler
End If
Resume Next

ErrHandler:
Dim cErr As New clsErrorLog
cErr.LogError Err.Number, Err.Description, "clsGeneral_OpenGlobalForm"
End Function
-------------------

Also, in each of my forms, I have create a 'Form Property':

-------------------
Public Property Set SourceRootObject(ByVal vData As Object)
On Error GoTo ErrHandler

Set mcContact = vData
txtID = mcContact.ID
If mcContact.ID < 1 Then mcContact.IsNew = True
Me.Caption = mcContact.ContactName & " - Personal Information"
LoadContactFields

On Error GoTo 0
Exit Property

ErrHandler:
Dim cErr As New clsErrorLog
cErr.LogError Err.Number, Err.Description, "frmContacts_SourceRootObject"
End Property
Public Property Get SourceRootObject() As clsContacts
Set SourceRootObject = mcContact
End Property
-------------------

The problem then is that the local variable (mcContact) in the form changes
each time I open a new instance.

So, when I open the first instance and set the variable to 'A', all is ok.
When I open the second (and subsequent) instance and set the variable to
'B', it remains 'B' in the second instance of the form, but the first
instance variable is not set to 'B'.

I have been banging my head against the wall for a week on this one. What am
I doing wrong?

Thanks for any help.
 
A

Allen Browne

That is (fortunately) how multiple instances work: each instance has its own
variables, its own current record, its own properties, and so on.

If you want a single variable instead of one for each instance, delcare your
variable in a standard module instead of the module of the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top