Results 1 to 6 of 6
  1. #1
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150

    VBA MS Access - A class within a form object - Retaining a class object

    Hi, this is rather an advanced Access programming topic in my opinion.. here goes..



    I wrote a class within a form module. The class is specific to the form's controls. The class properties manipulate the form to my desire.
    The intention of this class is to use the form as a subform. Think of the form as a custom control i can re-use to my hearts intent.
    I'm not a class expert - still learning so bare with me.

    Consider the example below:

    Inside the FORM Vba module:

    Code:
    Option Explicit
    
    Private VarSubformObject         As Form
    
    
    Public Property Set SubformObject(obj As Form)
         Set VarSubformObject = obj
    End Property
    
    
    Public Property Get SubformObject() As Form
         Set SubformObject = VarSubformObject
    End Property
    
    
    Private Sub Textbox1_Click()
        SubformObject.Controls("mybutton").Caption = "Hello World"
    End sub
    
    Public Sub Test()
     SubformObject.Controls("mybutton").Caption = "Hello World"
    End sub

    From the caller form - contains the object "mylistbox" as a subform control with a source object for the form in subject:

    Code:
    Option Explicit
    
    
    Private Sub Form_Load()
    
        Dim ctl As Form
        Set ctl = Forms("Main Form").mylistbox.Form
    
    
        Dim CustomList As Form_DragListboxControl 
        Set CustomList = New Form_DragListboxControl  'New instance
        
        With CustomList
            Set .SubformObject = ctl
                  .Test
    
       End With
    
    End sub
    The form load event in the caller works fine. The object sets and the test method will fire. However, when I physically click in the subform on Textbox1, the click event fires but fails to retain the source object I set in the form_load event. This is simply because after the form_load event terminates, the object is no longer set, correct?

    I get an error here:


    Inside the Form Module (class):
    Code:
    Private Sub Textbox1_Click()
        SubformObject.Controls("mybutton").Caption = "Hello World"      
    End sub
    I receive object variable or with variable not set.

    Is my logic correct that the object is being set to nothing when the caller's form_load event is terminated? If not, then i'm stuck on what to do!





    Thanks for any help. Appreciate your time.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,653
    Try putting a message box in the Class_Terminate event to test when the the event occurs.

    I have been dabbling in classes for a while and still trying to get a handle on them.
    I would try instantiating it in the general declarations of the form module. I think the class will persist while the form remains open.

    I wrote a class within a form module.
    Doesn't a class need to be in a class module?

    edit: A quick test confirmed that the class will initialize and terminate in the load event, so your logic is correct.

  3. #3
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    moke,

    thanks for your reply!

    This is a super confusing situation because of using a "class" inside a form module. I've seen it done prior to me doing it which is why i did it. If i am correct, a class inside a general module is not a true class so it's class events (Class Initialize, Class Terminate) are not possible because its a FORM. Class initialize does nothing and class terminate does nothing inside a form module.

    I am trying to get a handle on this class/form combo. Its confusing to me but at the end of the day, I am looking to using this class properties and methods to manipulate a specific subform control (form) on another form.

    I am going to try some things later on and will report back on finding. In mean time, welcome any thoughts on this approach?


    Regards

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,975
    You are correct that class events cannot be done in a standard module.
    However it is customary to do these in a class module if only so the events can be called from more than one place.
    Having said that a form module is a type of 'class module' so it may be that such code will work
    I've never tried doing so and can't be sure either way..
    Even if it does work, I don't see the benefit as its use will be restricted to within that form only.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,653
    Even if it does work, I don't see the benefit as its use will be restricted to within that form only.
    Colin's got a good point. Kinda defeats the purpose of a class.
    Unfortunately I had a lot of difficulty trying to instance a copy of a form from within a class so I doubt I can be of much help.
    I plan on re-visiting that issue so be sure to post your progress.

    I use one class often which is used to set default values and such in certain forms. In order for the scope to persist while the forms open I instantiate it in the general declarations of the form module. If you instantiate it in any other event or procedure the scope is limited to the scope of that event or procedure.

    Code:
    Option Compare Database
    Option Explicit
    
    
    Dim clsD As New clsDefaults

  6. #6
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Hi all,

    Thanks for replies....

    I simply just need a way to set these properties and call these methods to manipulate each individual copy of the form (subform). A class has to be the only way to do this. Whether that is within the form module itself or a separate class.

    To explain an example:

    Suppose i have two subforms "Subform1" and "Subform2"... both with this special form as source object. i would like to manipulate each form individually using a class.

    Lets consider the idea of a textbox control in a form... A textbox is an object you can manipulate its properties anywhere. You can call Textbox.Value anywhere in a module.... I want to be able to treat these subforms as their own control, just like the texxtbox.... Subform1.Value, .. .. .

    I'm starting to get concerned that i wrote an entire class (some 2000+ lines) in this form and won't be able to use it as i hoped. Any thoughts?

    thanks!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. cant delete class object
    By tagteam in forum Access
    Replies: 3
    Last Post: 12-14-2017, 10:07 AM
  2. Replies: 5
    Last Post: 07-21-2016, 12:36 PM
  3. Refer to class object in parent form
    By robs23 in forum Programming
    Replies: 0
    Last Post: 07-22-2015, 11:36 AM
  4. call class function from object
    By Ruegen in forum Programming
    Replies: 2
    Last Post: 01-20-2015, 09:51 AM
  5. Replies: 1
    Last Post: 04-26-2012, 12:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums