473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to pass listbox object from event procedure in Class Module

5
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:

Expand|Select|Wrap|Line Numbers
  1. 'Class Module code:
  2.  
  3. Option Explicit
  4.  
  5. ' Declare Event Handlers
  6. Public WithEvents BtnGrp As MSForms.CommandButton
  7. Public WithEvents LbxGrp As MSForms.ListBox
  8.  
  9. ' Declare Object to work with
  10. Dim oLstBx As MSForms.ListBox
  11.  
  12. Dim iEntryCnt As Integer
  13.  
  14. Sub LbxGrp_MouseUP(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  15.     Set oLstBx = LbxGrp
  16. End Sub
  17.  
  18. Private Sub BtnGrp_Click()
  19.     iEntryCnt = 0
  20.     Select Case BtnGrp.Name
  21.  
  22.         Case "okCmdBtn"
  23.             With oLstBx '**** -> This is where the problem occurs as oLstBx is "Nothing"
  24.                 .AddItem
  25.                 .List(iEntryCnt, 0) = iEntryCnt + 1
  26.                 .List(iEntryCnt, 1) = "OK was Clicked"
  27.             End With
  28.  
  29.         Case "cancelCmdBtn"
  30.             MsgBox "You Clicked " & BtnGrp.Name
  31.  
  32.         Case Else
  33.             Exit sub
  34.     End Select
  35.  
  36. End Sub
  37.  
  38.  
  39. 'UserForm module code:
  40.  
  41. Option Explicit
  42.  
  43. Dim Btns() As New Class1
  44. Dim Lbox() As New Class1
  45.  
  46.  
  47. Private Sub UserForm_Initialize()
  48.  
  49.     Dim BtnCnt As Integer: Dim LbxCnt As Integer
  50.     Dim ctl As MSForms.Control
  51.  
  52.     BtnCnt = 0: LbxCnt = 0
  53.  
  54.     For Each ctl In Me.Controls
  55.         If TypeName(ctl) = "CommandButton" Then
  56.             BtnCnt = BtnCnt + 1
  57.             ReDim Preserve Btns(1 To BtnCnt)
  58.             Set Btns(BtnCnt).BtnGrp = ctl
  59.  
  60.         ElseIf TypeName(ctl) = "ListBox" Then
  61.             With ctl
  62.                 LbxCnt = LbxCnt + 1
  63.                 .ColumnCount = 2
  64.                 .ColumnWidths = "1 cm;6 cm "
  65.                 ReDim Preserve Lbox(1 To LbxCnt)
  66.                 Set Lbox(LbxCnt).LbxGrp = ctl
  67.             End With
  68.         End If
  69.     Next ctl
  70.  
  71. End Sub
Thanks for the help in advance.
Feb 7 '13 #1
8 5131
zmbd
5,501 Expert Mod 4TB
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
Feb 7 '13 #2
ADezii
8,834 Expert 8TB
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.
    Expand|Select|Wrap|Line Numbers
    1. Private oLstBx As Access.ListBox
  2. Assign the Listbox to the Object Variable in the MouseUp() Event of the Listbox.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub LbxGrp_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.   Set oLstBx = LbxGrp
    3. End Sub
    4.  
  3. You can now Reference the Properties/Methods of the Object Variable anywhere within the confines of the Form's Class Module.
    Expand|Select|Wrap|Line Numbers
    1. With oLstBx
    2.   MsgBox "The Number of Items in " & .Name & " is " & .ListCount
    3. End With
  4. As zmbd has stated, you can also access it directly within the Form.
Feb 7 '13 #3
RojK
5
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.
Feb 7 '13 #4
RojK
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.
Feb 7 '13 #5
ADezii
8,834 Expert 8TB
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'.
Feb 7 '13 #6
RojK
5
Hmmm... Not sure how to fix this ADezii
Feb 7 '13 #7
ADezii
8,834 Expert 8TB
What, exactly, is the Container for your UserForm?
Feb 7 '13 #8
RojK
5
I am using it in a CAD software which uses VBA extension capability (with VBE). I access this through the APC6.0 reference lib.
Feb 7 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Enigman O'Maly | last post by:
I'm still somewhat new to object style programming (as will become evident), using VBA in Excel 2000 to automate some previously manual functions. I've defined a class module so that I can...
42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
2
by: Uninvisible | last post by:
I have put together a db for a law firm to keep track of counterfeit activities. There are four parent tables: tblContact tblTransaction tblAction tblFile I have created a form,...
2
by: kaushas | last post by:
All I have a file HTML object, I want to pass this object to a class so I can write a common method for uploading files. Idea is to avoid coding file saving in the forms submit button, but call a...
12
by: Adil Akram | last post by:
I want to cancel an event from its event procedure on some condition, I know how to do this in VB6 i.e. by setting cancel=true. Please tell me how can I do this in VB.net I don't know how to use...
3
by: Jason | last post by:
I have an ASP.NET application in which I would like to call my button click event (imgSubmitSearch_Click) on the page load if certain criteria are met. Is this possible? What is the correct...
1
by: Robert | last post by:
Every thing worked fine until all of a sudden, I get the following error whenever I try to do anything on one of my forms that calls an Event Procedure. I get the error as soon as the form opens...
1
by: superjacent | last post by:
Hope someone can point me in the right direction. When opening a form the 'click' event of the ListBox is invoked (run). I thought the 'click' event of the ListBox is only invoked when clicking...
5
by: MLH | last post by:
None of my applications' many forms have actions defined for the Form_Close event. I would lke to automate the addition of the following procedure to all my forms in their Close Event property...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.