Form instancing and an 'Object' variable

I

IcemanV1

I am a little confused. I have the following code:


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

Set cSource = vData
intRootObject = DLookup("ID", "tblRootObjects", "ObjectClass='" &
TypeName(cSource) & "'")
LoadRootObjects

On Error GoTo 0
Exit Property

ErrHandler:
Dim cErr As New clsErrorLog
cErr.LogError Err.Number, Err.Description,
"frmObjectMain_SourceRootObject"
End Property
Public Property Get SourceRootObject() As Object
Set SourceRootObject = cSource
End Property

This code resides in a form. I want to be able to open this form many times
over and so I use an instance of the form (Set frmToOpen = New
Form_frmLocationProductTypes) rather than DoCmd.OpenForm. OK. Easy enough.

Now when I create multiple instances of the form, the variable cSource
changes each time a form is instanced. So...
Create form Instance #1 and Set cSource to A
Create form Instance #2 and Set cSource to B
Create form Instance #3 and Set cSource to C

If I look at the value of cSource in Instance #1 and #2, they are set to
'C'. Why is that???

The forms are disconnected. There is no record source. It seems that the
object variables do not get their own memory space like other variables.
When you 'Set objA=objB' it seems that objB just refers to objA rather than
become a copy.
So.. I would like to know how to copy 1 object variable to another.


Here is the routine that adds the form to a global collection of forms:

Code:
Public Function OpenGlobalForm(OpenForm As Access.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


The above routine gets called from a number of other routines, but the
relevant code would be:

Code:
Private frmToOpen As Access.Form
..
..
..
Set frmToOpen = New Form_frmObjectMain
OpenGlobalForm frmToOpen, RootObject

It seems as though objects (RootObject variable in this case) is not being
created locally to the forms, but globally. I think (so confused, hehe)


Really confusing. Thanks for any assistance.
 

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