+ Reply to Thread
Results 1 to 9 of 9

Converting to Class Modules (Userform Controls)

  1. #1
    Registered User
    Join Date
    10-18-2012
    Location
    Surrey, BC
    MS-Off Ver
    Excel 2016
    Posts
    84

    Converting to Class Modules (Userform Controls)

    Hi there...
    I am at the point where my application is working how I would like it... I now am looking for ways to improve it piece by piece (It works good the way it is but I know it could be more efficient and coded better).

    One thing that keeps coming up is when I needed a little nudge from the experts is using class modules.

    What I am thinking and having trouble wrapping my head around is using class modules to run when certain controls are accessed.

    I have many different groups of controls (textboxes, listboxes, commandbuttons, labels) which are all organized by names.

    So I might have 100 textboxes named txtSample1 to txtSample100 and another set of textboxes named txtTest1 to txtTest32 and other various groups of controls.

    I want to be able to access a specific class module when one of the txtSample? is accessed and a different class module accessed when the txtTest? controls are accessed.

    I know how to check the controls to see if if it is a textbox control or commandbutton control by using control "type", I also know how to check the exact name, if it equals or not equal to txtTest3 do something by using the control name (by exact match).

    When using any of the txtSample1 - 100 textboxes, I would like to access the txtSample class and run the different events I need and to do things based on the txtSample 1-100 number.


    I currently have all my events for each of the txtSample1 -100 events (keydown, change, etc...) all leading to one procedure and works fine. I would like to get rid of all this excess code and have the controls access the correct class module.


    Can I do this?


    Craig
    Last edited by CraigsWorld; 05-09-2015 at 05:46 PM.

  2. #2
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Converting to Class Modules (Userform Controls)

    If I am understanding you correctly, I think what you want is a control array. Here is a link to a good tutorial for doing this: VBA – Control Arrays

  3. #3
    Registered User
    Join Date
    10-18-2012
    Location
    Surrey, BC
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Converting to Class Modules (Userform Controls)

    Ok, I have seen this sort of sample before and it is what I am looking for.... but this sample controls all textboxes on the userform.

    Lets say I have 100 textboxes:

    txtBlue1 - txtBlue10
    txtRed1 - txtRed10
    txtWhite1 - txtWhite10
    txtGreen1 - txtGreen10
    txtOrange1 - txtOrange10
    txtBlack1 - txtBlack10
    txtNone1 - txtNone10
    txtGray1 - txtGray10


    I would like to have a class module for each set based on the name... I don't want the textbox class doing something to all textboxes on the userform.
    The txtBlue1 - 10 would access clsBlue and so on... can this be done? This is what I am not able to find info on...

    Basically each class set would be doing something completely unrelated to any of the other textboxes...

    Thanks
    Craig

  4. #4
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Converting to Class Modules (Userform Controls)

    Quote Originally Posted by CraigsWorld View Post
    Ok, I have seen this sort of sample before and it is what I am looking for.... but this sample controls all textboxes on the userform.
    ...
    I would like to have a class module for each set based on the name... I don't want the textbox class doing something to all textboxes on the userform.
    The txtBlue1 - 10 would access clsBlue and so on... can this be done? This is what I am not able to find info on...

    Basically each class set would be doing something completely unrelated to any of the other textboxes...

    Thanks
    Craig
    So create your clsBlue and only add those textboxes whose name matches that pattern
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-18-2012
    Location
    Surrey, BC
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Converting to Class Modules (Userform Controls)

    How can I do this for all the txtBlues? What happens if there are 100 of them... would be a very large "if" statement.

  6. #6
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Converting to Class Modules (Userform Controls)

    Then the if condition that I previously provided based on you example would be:
    Please Login or Register  to view this content.
    This can handle txtBlue1 thru txtBlue999.

    Alternatively you could extract the numeric portion of the name and use a Select Case statement, but that is probably more effort than it is worth.
    Last edited by Leith Ross; 05-10-2015 at 12:43 AM.

  7. #7
    Registered User
    Join Date
    10-18-2012
    Location
    Surrey, BC
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Converting to Class Modules (Userform Controls)

    Using test code from the class array earlier, once I placed the if(name like statement in... it no longer works.


    I added it in the below code: based on the original sample further down... I am accessing it correctly?
    Please Login or Register  to view this content.


    The original code

    Class1
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Converting to Class Modules (Userform Controls)

    Quote Originally Posted by CraigsWorld View Post
    I added it in the below code: based on the original sample further down... I am accessing it correctly?
    Please Login or Register  to view this content.
    Your pattern definition is wrong.
    Please Login or Register  to view this content.
    is equivalent to:
    Please Login or Register  to view this content.
    You need a wild-card at the end of "TextBox",
    Please Login or Register  to view this content.
    This would match any name that starts with "TextBox" followed by any number of characters.

    In your code, select the word "Like" and press the F1 key. This will bring up the help on the "Like Operator" that you should read.

  9. #9
    Registered User
    Join Date
    10-18-2012
    Location
    Surrey, BC
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Converting to Class Modules (Userform Controls)

    Ok... now I think I have something to work with! That did the trick. I had never heard of the like operator before.

    Thanks for your help... now to try placing this in my code.

    Craig

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Userform & Dynamic Controls & Class Module
    By sarndt01 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-18-2014, 01:53 PM
  2. Replies: 1
    Last Post: 10-12-2012, 04:36 AM
  3. Class Modules Help
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2009, 06:45 AM
  4. PROBLEM: Keywords;= Userform, Class Modules, ButtonGroup and Nomenclature
    By Myles in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-01-2006, 08:50 PM
  5. Basic question - modules and class modules - what's the difference?
    By Mark Stephens in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-08-2005, 07:08 AM

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