+ Reply to Thread
Results 1 to 9 of 9

Public Sub for Userform Change Events

  1. #1
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Public Sub for Userform Change Events

    SITUATION: I have a userform (A_MAIN) that displays a few command buttons. If the user presses the command button "Add" an additional userform (B_ADD) populates or if the user presses the command button "Modify" a different userform (C_MODIFY) populates. While each userform serves a separate and distinct function, they nevertheless share one thing in common - they all use four combo boxes (CB_FUNC, CB_COMP, CB_BLN, CB_FLN) and a textbox (TB_KEY) in the exact same way. That is, the user selects an option from a given combobox and the "key" lookup value generates in TB_KEY. With these look up values, I can find anything in my data tables (see the "parts" tab).

    PROBLEM: As it stands now, I copied and pasted the change events and initialization routines into each userform. While that works fine, it creates a lot of redundant coding (it’s now in three different places). I would like to centralize the code into one place. Please see the attached file to see more (NOTE: I got rid of any additional coding that is not directly involved in this problem in the attached file, I hope that helps).

    ATTEMPTED SOLUTION: I created a module and tried to create a public sub for the initalization and each change event to call. It doesn't work the way I intended…any help would be greatly appreciated. The redundant coding, used in each userform, is as follows:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: Public Sub for Userform Change Events

    Combine your second and third userforms into one, and hide/unhide the controls as needed depending on the choice made on the first userform, along the lines of:

    Please Login or Register  to view this content.

    You can also pass the userform object to your subroutines, and use that object instead of Me or a named userform.

    Please Login or Register  to view this content.
    ....
    Last edited by Bernie Deitrick; 10-09-2018 at 03:08 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Public Sub for Userform Change Events

    You could make a sub (in some Module) like this:
    Please Login or Register  to view this content.
    Then call that sub from the userforms_initialize procedures with
    Please Login or Register  to view this content.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Public Sub for Userform Change Events

    As a matter of fact the Initialize-procedure can be shorter still.
    Please Login or Register  to view this content.
    Calling it remains the same.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Re: Public Sub for Userform Change Events

    Quote Originally Posted by Bernie Deitrick View Post
    Combine your second and third userforms into one, and hide/unhide the controls as needed depending on the choice made on the first userform, along the lines of:

    Please Login or Register  to view this content.

    You can also pass the userform object to your subroutines, and use that object instead of Me or a named userform.

    Please Login or Register  to view this content.
    ....
    Bernie,
    After thinking about it for a while, I think combining userform 2 and 3 will be very doable for me. I'll have to play around with it for a little bit, but I'm sure that will be the best way. Thank you for the idea. As far as using the same code for each of the comboboxes (CB_FUNC, CB_COMP, etc.) between userform 1 and 2 (MAIN, and now ADD/MODIFY) it looks like you define uForm as an object that can be used the same way when running either userform because the only thing that is different between userform 1 and 2, when it comes to the combo boxes, is the userform name. Hopefully I understood that correctly. I'll have to spend some time tomorrow playing around with it.

  6. #6
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Re: Public Sub for Userform Change Events

    Quote Originally Posted by Tsjallie View Post
    As a matter of fact the Initialize-procedure can be shorter still.
    Please Login or Register  to view this content.
    Calling it remains the same.
    Thanks Tsjallie for doing this. I'll have to play around with this tomorrow. While the initialize routine works well, do I simply follow the same pattern with a ComboBox change routine since the only thing that is different between the code is the userform name? Thanks for all the help!

  7. #7
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Public Sub for Userform Change Events

    With some minor changes (variable declarations) you can have the same concept for handling change events for the comboboxes.
    Add this sub to Module1:
    Please Login or Register  to view this content.
    Call the sub from the change event procedures with this line:
    Please Login or Register  to view this content.
    Substitute combobox name with the objectname of the combobox.

  8. #8
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Re: Public Sub for Userform Change Events

    Thanks Tsjallie! I think it is much cleaner.

  9. #9
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Public Sub for Userform Change Events

    Glad I could help. Thx for the rep

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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] Public Variable between Sub and Userform
    By BrianAll in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-22-2018, 07:34 AM
  2. [SOLVED] How to call a public sub function from either a userform or another sub
    By daustin5 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-14-2016, 04:16 PM
  3. Userform not passing through 1 particular public variable
    By newbvba in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2014, 09:59 PM
  4. Using public function from userform
    By Geronimo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2010, 08:53 AM
  5. Calling a public function from userform
    By BBQNJL in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-09-2009, 05:38 PM
  6. Lost public label in userform
    By joneswesley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2009, 07:14 AM
  7. [SOLVED] Pass public variable from one userform to a second...
    By Mike Dunworth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2005, 08:05 PM

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