I have a main form with a register and a few subforms. I’m using class modules and I save the name of the forms in it, to make the access to them easy. The corresponding variable to access the class gets saved in a module and is set (new) on_load in a form (clsMod). Before the first access my main form calls a function which ‘initializes’ the values in the class module (initial_form), to make them accessible. That works like a charm, so far.
But when I now try to access the value, f.e. with clsMod.detailsControl or clsMod.detailsControl!fieldXy my class module gets initialized again a thus looses all bound objects. I suppose I am not allowed to use Controls / Forms like that? There is no error, except of course ‘Object variable, or with block variable not set’, which occurs afterwards.
Private m_ctldetailsControl As control
Public Sub initial_form()
Set detailsControl = Forms!mainForm_ufoMainForm
End Sub
Public Property Get detailsControl() As control
Set detailsControl = m_ctldetailsControl
End Property
Public Property Set detailsControl(ctlDetailsControl As control)
Set m_ctldetailsControl = ctlDetailsControl
End Property
I narrowed it down to the fact that the class module just gets initialized again, when I access the control-object from the ‘outside’ (I put a timestamp in the Class_Initialize() and can see when there is a new initialization), I just don’t know why. Same happens when I use Form-Objects instead of Control-Objects.
I can eliminate the my code resets the class-module, because it only gets set once during the load process (set clsMod = new clsModification). Everything else inside that class works fine, I can access the property from inside the class without reinitializing itself.
Any ideas or further reading regarding this topic would be greatly appreciated, for any other details just ask!
A few additions:
- The class variable is located as “public clsMod As clsModuleXy” in a module
- it gets set in the onLoad Event of my form (set clsMod = new clsModuleXy)
- set-property works fine (as descriped above)
- get property works fine inside the class-module (as descriped above)
- when I use the get property outside of the class module a new instantiation happens (if I set a local control/form to that property or want to access a field)
I’m guessing that the culprit is that you have declared an instance of this class module
As New. I obviously don’t know what the rest of your code looks like, but I imagine the whole process is working something like this:As New(ie,Dim clsMod As New initial_form).clsMod) is created.clsMod) of the object to go out of scope.clsMod) which is no longer in use.clsMod) is cleaned up.clsMod. That variable is Nothing because the GC cleaned it up. However, you declared itAs Newso a brand new instance ofinitial_formis created and assigned to the object variableclsMod.Without seeing the rest of your code, I can’t say for sure this is the problem. But based on the symptoms you posted, this would explain the behavior.