+ Reply to Thread
Results 1 to 5 of 5

Is it possible to assign a userform NAME to a variable and reference the form?

  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Is it possible to assign a userform NAME to a variable and reference the form?

    Hi,

    I've drawn a bit of a blank with this one.

    I would like to assign a form username to a variable that I can then reference from another module. The reason for this is that I have a few userforms that are slightly different in terms of content and layout but share many similarities in function. To keep everythign a bit neater i'd like to hold most of the 'doing' code within a module and then call it as required from the userform code.

    I understand that I can .show a userform via this method, but not sure if it's possible to reference it for other purposes - e.g. populating a combobox list.

    The code setup I have at present:

    Userform (TransForm1):
    Please Login or Register  to view this content.
    Module1:
    Please Login or Register  to view this content.
    This in itself doesn't trigger any runtime errors (though it does trigger the userform initialise event again when it reaches 'Set CurrForm', I have a bit of code to work around that) but when it comes to the Case Select statement it doesn't register any of the cases as true - leading me to suspect that it isn't carrying over the Userform name properly. This code ran fine when the POSType sub was within the userform module.

    So any ideas? Can it be done? Or do I need to just copy and paste the code and live with it being messy?

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Is it possible to assign a userform NAME to a variable and reference the form?

    replace
    Please Login or Register  to view this content.
    by
    Please Login or Register  to view this content.
    note this is not really good design in my opinion. you ought to either pass the control you want filled as an argument to the routine or change the routine to a function that returns the array you want assigned to the list
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Is it possible to assign a userform NAME to a variable and reference the form?

    That works great thanks JP and doesn't trigger the initialise even either.

    Why do you feel this is not good design? Not trying to be difficult, just want to understand.
    How would I pass the control (guessing that would also include the form it resides in) and yet also let that control to be +1'd? I need to check the case of the first combobox and then update the second, applying to any one of ten 'pairs' of comboxes, across multiple userforms?

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Is it possible to assign a userform NAME to a variable and reference the form?

    it's not good design because
    • you are assuming the names of the controls in the routines which is unnecessary and awkward
    • you're using public variables which should be avoided when possible-and it's certainly possible here
    • you mention slight differences in layout and content which makes me suspect that one form with some dynamic content might be a better option than multiple similar forms

    it also sounds as though using a class (or a few) to handle the linked controls might be an option

    you could alter your routine to take two controls as arguments then pass both from each calling form

    Please Login or Register  to view this content.
    and call with
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Is it possible to assign a userform NAME to a variable and reference the form?

    Thanks again JP. Works perfectly as I didn't doubt.

    Okay I think I understand this. So I should pass control names within the brackets to the called sub as and when required rather than declaring them publicly.
    I have a fair few public variables within my code as that was the only way I knew how to share variable data between different subroutines. I'm guessing I could get rid of a lot of these using a similar method to that above. I will review my code and see what tidying I can do. If I get stuck then I'll do some more searching on passing arguments.

    Regarding point three - you are probably right on that count - but I'm fairly new to this and there's only so much info I can take in and make stick at one time. I'll leave dynamic content creation for another day.

    Thanks again, TC

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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