How to pass listbox object from event procedure in Class Module

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RojK
    New Member
    • Feb 2013
    • 5

    #1

    How to pass listbox object from event procedure in Class Module

    Hi,

    I have a userform which has some command buttons and multiple listboxes in VBA.

    I have a class module which handles events on the form, and a code in the form's initialization section which sets the reference to the class module (depending on which control is clicked).

    What I am struggling with, is as follows:

    When the user clicks on a listbox, it is enabled. This is fine so far. The problem starts when I want to capture this listbox within this event procedure, and pass it onto another procedure as the listbox object.

    In this way, I want to have the selected listbox's methods and properties available to the other procedure.

    When I run in debug mode, I notice that eventhough I have declared another listbox object as a global object variable, to which I assign the selected listbox, the object value goes to "Nothing" after the event is over.

    How can I do this? Is it possible?

    The code is as follows:

    Code:
    'Class Module code:
    
    Option Explicit
    
    ' Declare Event Handlers
    Public WithEvents BtnGrp As MSForms.CommandButton
    Public WithEvents LbxGrp As MSForms.ListBox
    
    ' Declare Object to work with
    Dim oLstBx As MSForms.ListBox
    
    Dim iEntryCnt As Integer
    
    Sub LbxGrp_MouseUP(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Set oLstBx = LbxGrp
    End Sub
    
    Private Sub BtnGrp_Click()
        iEntryCnt = 0
        Select Case BtnGrp.Name
    
            Case "okCmdBtn"
                With oLstBx '**** -> This is where the problem occurs as oLstBx is "Nothing"
                    .AddItem
                    .List(iEntryCnt, 0) = iEntryCnt + 1
                    .List(iEntryCnt, 1) = "OK was Clicked"
                End With
    
            Case "cancelCmdBtn"
                MsgBox "You Clicked " & BtnGrp.Name
    
            Case Else
                Exit sub
        End Select
        
    End Sub
    
    
    'UserForm module code:
    
    Option Explicit
    
    Dim Btns() As New Class1
    Dim Lbox() As New Class1
    
    
    Private Sub UserForm_Initialize()
    
        Dim BtnCnt As Integer: Dim LbxCnt As Integer
        Dim ctl As MSForms.Control
        
        BtnCnt = 0: LbxCnt = 0
        
        For Each ctl In Me.Controls
            If TypeName(ctl) = "CommandButton" Then
                BtnCnt = BtnCnt + 1
                ReDim Preserve Btns(1 To BtnCnt)
                Set Btns(BtnCnt).BtnGrp = ctl
                
            ElseIf TypeName(ctl) = "ListBox" Then
                With ctl
                    LbxCnt = LbxCnt + 1
                    .ColumnCount = 2
                    .ColumnWidths = "1 cm;6 cm "
                    ReDim Preserve Lbox(1 To LbxCnt)
                    Set Lbox(LbxCnt).LbxGrp = ctl
                End With
            End If
        Next ctl
    
    End Sub
    Thanks for the help in advance.
    Last edited by zmbd; Feb 7 '13, 02:36 PM. Reason: [Z{Please use the <CODE/> button to format posted code/html/sql and Read the FAQ}[Took out some weird extra spacing}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    RojK:
    Why again are you using a class module for a listbox on a form?

    If you are using the listbox's selected value for other controls within the form, then you do not need a class module. The control, it's current value, state, and properties are available within the scope of the form. If you need to pass that value to another sub within a standard module then you can code the routine to have arguments within which to pass the control's value.

    This is a must read: Posting Guidelines

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      The problem appears to be the assignment of the Object Variable (oLstBx) in LbxGrp_MouseUP( ). It may not be able to be assigned in this manner within the context of a Userform. I am not working with a UserForm, but comparable Code in Access VBA would be.
      1. Declare the Object Variable in the General Declarations Section of the Form.
        Code:
        Private oLstBx As Access.ListBox
      2. Assign the Listbox to the Object Variable in the MouseUp() Event of the Listbox.
        Code:
        Private Sub LbxGrp_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
          Set oLstBx = LbxGrp
        End Sub
      3. You can now Reference the Properties/Methods of the Object Variable anywhere within the confines of the Form's Class Module.
        Code:
        With oLstBx
          MsgBox "The Number of Items in " & .Name & " is " & .ListCount
        End With
      4. As zmbd has stated, you can also access it directly within the Form.

      Comment

      • RojK
        New Member
        • Feb 2013
        • 5

        #4
        Hi zmbd,

        Thanks for your kind and prompt reply.

        Firstly, sorry about not following the posting guidelines... I had made a rushed attempt... desperate to find a solution.

        To answer your question:

        The userform is created dynamically based on some input from the user. The number of listboxes correspond to this user input.

        The idea is to allow the user to enable the first listbox, add or remove data to or from it by using the relevant command buttons, and then move onto the next listbox..etc.

        I was hoping to use the class to reference the selected listbox and then manipulate the data in it.

        I hope I am making sense.

        Comment

        • RojK
          New Member
          • Feb 2013
          • 5

          #5
          Thank you ADezii.

          I have tried this already, but the oLstBx object sets to "Nothing" the moment the event is over. This prevents me from accessing its methods and properties outside the event, but yet, within the context of the class module.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            the oLstBx object sets to "Nothing" the moment the event is over.
            If it is truly Initialized within the Event, but is set to 'Nothing' outside the Event, then this would indicated a 'Scope' problem - the extent to which the Object Variable is 'Visible'.

            Comment

            • RojK
              New Member
              • Feb 2013
              • 5

              #7
              Hmmm... Not sure how to fix this ADezii

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                What, exactly, is the Container for your UserForm?

                Comment

                • RojK
                  New Member
                  • Feb 2013
                  • 5

                  #9
                  I am using it in a CAD software which uses VBA extension capability (with VBE). I access this through the APC6.0 reference lib.

                  Comment

                  Working...