+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 23

Passing combobox value to custom class

  1. #1
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Passing combobox value to custom class

    Hi all,

    I have created a class that populates a combobox list with my values for reuase, this class is then initialised in a user form and linked to a combobox1.

    However, I'm trying to generate a couple of properties from the selection of this list made by the user.

    So, to try and explain again, the list is created by a class, the user clicks on the drop down and selects a value in the list, I then wnat the class to know which value is selected so that I can do calculations in the class.

    How do I do this?

    Here is my code:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by Jacques Grobler; 07-05-2012 at 03:15 AM.
    Jacques


  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Passing combobox value to custom class

    In the class

    Please Login or Register  to view this content.
    In the form

    Please Login or Register  to view this content.
    Now when changing a value in the combobox the class variable cb_value will hold the selected value
    Please take time to read the forum rules

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003, 2007, 2010 & 2013
    Posts
    18,916

    Re: Passing combobox value to custom class

    The class object already knows.

    Within the class object
    Please Login or Register  to view this content.
    And you can use the events of the m_cmbFlangeSection object to code against.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Hi Andy, what would my new code look like?

    Your code:

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003, 2007, 2010 & 2013
    Posts
    18,916

    Re: Passing combobox value to custom class

    As you have declared the reference in the class as public you can use the following in your userform

    Please Login or Register  to view this content.
    If it had been a private member of the class you could add a property to the class
    Please Login or Register  to view this content.
    Userform code
    Please Login or Register  to view this content.
    Is the sole purpose of this to populate a list with fixed values? If so a simple function would have done.

  6. #6
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Hi Andy

    Sorry but I'm not getting it to work.

    Here is how I have applied your code to my code:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I'm getting a run-time 91 error on:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Hi Steffen,

    I'm also not able to get your code to work, not sure why.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003, 2007, 2010 & 2013
    Posts
    18,916

    Re: Passing combobox value to custom class

    It's really not clear why you are doing this.
    It's also very confused code.

    You have declared the object m_cmbFlangeSection but then not used it, which is the reason for my code error.

    When I change the reference in the class and modify the userform code it works.

    I've removed the line where you declare a new instance of the object.
    Value is not a property available for generic msforms.Controls.

    Please Login or Register  to view this content.
    But the issue now is why the class when you are coding against the combox events on the userform. As I said if all you want to do is load the control with a static list use a function in a standard code module. Or as Romperstomper suggested originally read the information from a file.

  9. #9
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    The combobox is used in a Engineering form. The user selects a value in the combobox and then the class goes through a series of calculations, calculating the running mass (kg/m), the paint area (mē), etc. These properties are then returned to the user form for further calculations.

    The custom combo, just like some of the others I am/ will be working on, is to be used on various types of Engineering forms, each calculating something else. The excel file (before the user uses it) will be a template file, stored under our templates folder, and then when changed into a normal .xlsm file, be moved to the relevant project folder.

    The reason why I dont want to have the combobox reference to a stand-alone file, is that, if the structure of our system changes, or we realise that our designs are not optimum and we then decide to change the design calculations, it will influence existing designs, and I'd like to keep what we have designed at a specific time, to remain as is, no matter what other decision is later made.

    As for the code change you suggest, I'll try now to see if it works.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003, 2007, 2010 & 2013
    Posts
    18,916

    Re: Passing combobox value to custom class

    So you need to move your calculation code into the class code using the events of the object your declared but as yet are not using.

    There should be no need for code in the combobox on the user forms events.

    Once you have done the class level calculations you can raise your own events in the class and capture those in the userform.

  11. #11
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Damn it is nice to talk to the experts, and sorry for not quit explaining what I want to achieve in words that all can understand.

    How would I achieve this?

  12. #12
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    I would for instance take the selection and split the string, remove what is standing in the front, and split at the "x", giving me three values, these I would use in calculations.

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003, 2007, 2010 & 2013
    Posts
    18,916

    Re: Passing combobox value to custom class

    class code
    Please Login or Register  to view this content.
    Userform code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Thanks for the reply Andy, it seems to do something like what I'd like to achieve, I'm going off-line for a bit (work is calling), but I will post again in this thread, even if it is just to say SOLVED...

    Thanks again.

  15. #15
    Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    2007, 2010, 2016
    Posts
    275

    Re: Passing combobox value to custom class

    Morning guys,

    So I have made a couple of adjustments, and it works perfectly, thank you very much.

    Here is the final code as I will be using it in my forms:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    For now I'll say this is solved, but if anyone can point out if I can "clean up" my code, please feel free to do so.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1