Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / May 2005

Tip: Looking for answers? Try searching our database.

Form Instance and Objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
IcemanV1 - 31 May 2005 16:44 GMT
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.
Allen Browne - 31 May 2005 17:55 GMT
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.

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

>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
[quoted text clipped - 95 lines]
>
> Thanks for any help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.