+ Reply to Thread
Results 1 to 11 of 11

transfer variable values between user forms?

  1. #1
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    transfer variable values between user forms?

    I have a combo box that selects the name of a company, and based the company I have another combo box that lets you select and account number.

    After you make the 2 selections I have another userform that comes up where you can edit data based on the company and account number.

    How can you pass what you selected from the first userform to the second? I made a global variable to record the company selection in the first userform, but how do you transfer it to the second? Is there a better way to do it?

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    re: transfer variable values between user forms?

    This depends a bit on how you have the first and second forms related to each other. Does the first form "Show" the second form? It is generally not best to use global variables if you can avoid them. If you define a function/sub on the second form, the first one can pass the data to the second through this routine after the second form is loaded.

    If you have some sample code, please post it.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    re: transfer variable values between user forms?

    Quote Originally Posted by blane245 View Post
    This depends a bit on how you have the first and second forms related to each other. Does the first form "Show" the second form? It is generally not best to use global variables if you can avoid them. If you define a function/sub on the second form, the first one can pass the data to the second through this routine after the second form is loaded.

    If you have some sample code, please post it.
    Yes, the first form shows the second one when you press "OK". Can you explain how defining a function on the second form allows you to transfer the variable values from the first form?

    I do have sample code, but I'm not sure how to post it. Last time I posted code it said that it did not conform to forum guidelines.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: transfer variable values between user forms?

    I can't see why you need two Userforms. It can easily be done with one
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: transfer variable values between user forms?

    royUK may have the best solution for your needs. If you still feel that it is best to use 2 forms, attach a sample workbook to your reply.

  6. #6
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    Re: transfer variable values between user forms?

    Quote Originally Posted by blane245 View Post
    royUK may have the best solution for your needs. If you still feel that it is best to use 2 forms, attach a sample workbook to your reply.
    I think you're right, that it can be done with 1 forum. What I'm struggling with now is taking the information from the second combo box and then putting it into the subs to generate the value that goes into the third combo box.

    The problem is that the 2nd box is dependent on the 1st, and the 3rd is dependent on the 2nd. When I'm changing the first box it immediately selects a value for the second, which triggers an event on the 3rd. I only want that event to happen if the user selects a value from the second combo box.

    What do I need to put around the code in order to post it?
    Last edited by max3732; 04-12-2010 at 04:32 PM.

  7. #7
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    Re: transfer variable values between user forms?

    also, I would post code if I knew how to use the tags
    Last edited by max3732; 04-12-2010 at 10:23 PM.

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: transfer variable values between user forms?

    To use the code tags, you type [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] after it.

    To use 2 forms for this:
    Let's say your forms are Userform1 and Userform2. In Userform2, add two public variables - for example:
    Please Login or Register  to view this content.
    then in your first form you can use:
    Please Login or Register  to view this content.
    and your second form can then simply refer to Company and Account as needed.

    If you have one form and want to selectively disable event processing, then you need a boolean flag declared at the top of the module:
    Please Login or Register  to view this content.
    then the first line of all of your event routines should be:
    Please Login or Register  to view this content.
    Then when you want to suppress events being triggered by another routine, you simply set the variable to true:
    Please Login or Register  to view this content.
    Does that make sense?
    Remember what the dormouse said
    Feed your head

  9. #9
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    Re: transfer variable values between user forms?

    I put everything on one userform, but I'm struggling to transfer data from the userform back to a module. Here is what I have in the top of the userform called "CardType":

    [code]

    Public CardValue As Double

    /code

    The userform also has a sub called "CardNumber"In the module I put

    [code]

    Dim frm As CardType
    Set frm = CardType
    frm.CardValue = CardNumber.Text
    frm.Show

    /code

    When I try that it says "variable not defined" when referring to cardnumber

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: transfer variable values between user forms?

    If CardNumber is a sub, it has no properties so you cannot refer to its Text property.
    Also, your code should be:
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    Re: transfer variable values between user forms?

    thanks for your help!

+ Reply to Thread

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