+ Reply to Thread
Results 1 to 10 of 10

UNDERSTANDING VARIABLE SCOPE

  1. #1
    -JEFF-
    Guest

    UNDERSTANDING VARIABLE SCOPE

    I am trying to maintain variables value so when I return to a userform that
    has already been used, it will already be filled out (as long as the user has
    previously visited it. I thought declaring the variables as public at the
    module level would make them visible in all subs but it is not working.

    In module 3 I have variables declared as public. In the sub, the variables
    are not declared because I want to use to publics but they seem to get over
    written when they are assigned a value. ie.

    MODULE 3
    public v1 as string
    public v2 as string

    private sub MyButton_click()
    v1 = "myValue"
    v2 = "myValue"
    end sub

    The watch window shows <Out of context> after values have been assigned
    inside the sub.


  2. #2
    Jim Thomlinson
    Guest

    RE: UNDERSTANDING VARIABLE SCOPE

    The values in the form are being discarded because you are unloading the
    form. Instead of unloading the form just hide it. By doing this the values
    will not be lost and you will not need to store them anywhere. The next time
    you show the forms it look just like th user left it before it was hidden.
    --
    HTH...

    Jim Thomlinson


    "-JEFF-" wrote:

    > I am trying to maintain variables value so when I return to a userform that
    > has already been used, it will already be filled out (as long as the user has
    > previously visited it. I thought declaring the variables as public at the
    > module level would make them visible in all subs but it is not working.
    >
    > In module 3 I have variables declared as public. In the sub, the variables
    > are not declared because I want to use to publics but they seem to get over
    > written when they are assigned a value. ie.
    >
    > MODULE 3
    > public v1 as string
    > public v2 as string
    >
    > private sub MyButton_click()
    > v1 = "myValue"
    > v2 = "myValue"
    > end sub
    >
    > The watch window shows <Out of context> after values have been assigned
    > inside the sub.
    >


  3. #3
    Bob Phillips
    Guest

    Re: UNDERSTANDING VARIABLE SCOPE

    Hey Jim,

    That shouldn't be it. It would be true if the variables were declared in the
    form, but if the variables are declared in a normal code module, as the OP
    states, their scope extends beyond the life of the form.

    It sounds as if the OP has also declared them as class variables in the
    form, and so the form uses them, but that is just a guess.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:7A312F4B-3374-47F6-B3E7-48A4AF22BC07@microsoft.com...
    > The values in the form are being discarded because you are unloading the
    > form. Instead of unloading the form just hide it. By doing this the values
    > will not be lost and you will not need to store them anywhere. The next

    time
    > you show the forms it look just like th user left it before it was hidden.
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "-JEFF-" wrote:
    >
    > > I am trying to maintain variables value so when I return to a userform

    that
    > > has already been used, it will already be filled out (as long as the

    user has
    > > previously visited it. I thought declaring the variables as public at

    the
    > > module level would make them visible in all subs but it is not working.
    > >
    > > In module 3 I have variables declared as public. In the sub, the

    variables
    > > are not declared because I want to use to publics but they seem to get

    over
    > > written when they are assigned a value. ie.
    > >
    > > MODULE 3
    > > public v1 as string
    > > public v2 as string
    > >
    > > private sub MyButton_click()
    > > v1 = "myValue"
    > > v2 = "myValue"
    > > end sub
    > >
    > > The watch window shows <Out of context> after values have been assigned
    > > inside the sub.
    > >




  4. #4
    Jim Thomlinson
    Guest

    Re: UNDERSTANDING VARIABLE SCOPE

    Not how I read the question but who knows. My understanding was that the OP
    wanted the values that last populated a form to persist until the next time
    the form was shown. If the form is unloaded then the values are lost. This
    being the case the OP wanted to store those values globally and then populate
    the form with these stored values the next time the form was shown. If the
    form is hidden instead of unloaded tehn the values are not lost and there is
    no need to store the values globally. But is is a little unclear and I see
    your point.

    Jeff if we have not answered your question or if you are more confused than
    when you first asked just reply back and perhaps we can get you on track.
    --
    HTH...

    Jim Thomlinson


    "Bob Phillips" wrote:

    > Hey Jim,
    >
    > That shouldn't be it. It would be true if the variables were declared in the
    > form, but if the variables are declared in a normal code module, as the OP
    > states, their scope extends beyond the life of the form.
    >
    > It sounds as if the OP has also declared them as class variables in the
    > form, and so the form uses them, but that is just a guess.
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > news:7A312F4B-3374-47F6-B3E7-48A4AF22BC07@microsoft.com...
    > > The values in the form are being discarded because you are unloading the
    > > form. Instead of unloading the form just hide it. By doing this the values
    > > will not be lost and you will not need to store them anywhere. The next

    > time
    > > you show the forms it look just like th user left it before it was hidden.
    > > --
    > > HTH...
    > >
    > > Jim Thomlinson
    > >
    > >
    > > "-JEFF-" wrote:
    > >
    > > > I am trying to maintain variables value so when I return to a userform

    > that
    > > > has already been used, it will already be filled out (as long as the

    > user has
    > > > previously visited it. I thought declaring the variables as public at

    > the
    > > > module level would make them visible in all subs but it is not working.
    > > >
    > > > In module 3 I have variables declared as public. In the sub, the

    > variables
    > > > are not declared because I want to use to publics but they seem to get

    > over
    > > > written when they are assigned a value. ie.
    > > >
    > > > MODULE 3
    > > > public v1 as string
    > > > public v2 as string
    > > >
    > > > private sub MyButton_click()
    > > > v1 = "myValue"
    > > > v2 = "myValue"
    > > > end sub
    > > >
    > > > The watch window shows <Out of context> after values have been assigned
    > > > inside the sub.
    > > >

    >
    >
    >


  5. #5
    -JEFF-
    Guest

    RE: UNDERSTANDING VARIABLE SCOPE

    Will try that. I unload it because I thought I had to in order to open up
    other forms dependingon what buttons are clicked. How do I create a public
    variable that is visible to all of the userform control subs? I have been
    coding all of my VBA in the userform control subs, would it be better
    practice (or easier to control) to have the click() subs call procedures in a
    module and then return?
    -JEFF-

    "Jim Thomlinson" wrote:

    > The values in the form are being discarded because you are unloading the
    > form. Instead of unloading the form just hide it. By doing this the values
    > will not be lost and you will not need to store them anywhere. The next time
    > you show the forms it look just like th user left it before it was hidden.
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "-JEFF-" wrote:
    >
    > > I am trying to maintain variables value so when I return to a userform that
    > > has already been used, it will already be filled out (as long as the user has
    > > previously visited it. I thought declaring the variables as public at the
    > > module level would make them visible in all subs but it is not working.
    > >
    > > In module 3 I have variables declared as public. In the sub, the variables
    > > are not declared because I want to use to publics but they seem to get over
    > > written when they are assigned a value. ie.
    > >
    > > MODULE 3
    > > public v1 as string
    > > public v2 as string
    > >
    > > private sub MyButton_click()
    > > v1 = "myValue"
    > > v2 = "myValue"
    > > end sub
    > >
    > > The watch window shows <Out of context> after values have been assigned
    > > inside the sub.
    > >


  6. #6
    Jim Thomlinson
    Guest

    RE: UNDERSTANDING VARIABLE SCOPE

    If the form is not unloaded then you do not need to store the value. You can
    just have the one form reference the values of the other form. Something like

    userform2.textbox1.text = userform1.textbox1.text

    This works so long as user form 1 is loaded. If not then it will throw an
    error. If you want to use global variables to store values they should be
    created (most likely) in modules. This is because modules run for the
    duration of the program. Forms will go in and out of existence and globals
    will be created and destroyed with the form. That being said if the global
    only makes sense if the form is in existence then put it in the form. It will
    only be created with the form and end with the form and this is a more
    efficent use of memory.
    --
    HTH...

    Jim Thomlinson


    "-JEFF-" wrote:

    > Will try that. I unload it because I thought I had to in order to open up
    > other forms dependingon what buttons are clicked. How do I create a public
    > variable that is visible to all of the userform control subs? I have been
    > coding all of my VBA in the userform control subs, would it be better
    > practice (or easier to control) to have the click() subs call procedures in a
    > module and then return?
    > -JEFF-
    >
    > "Jim Thomlinson" wrote:
    >
    > > The values in the form are being discarded because you are unloading the
    > > form. Instead of unloading the form just hide it. By doing this the values
    > > will not be lost and you will not need to store them anywhere. The next time
    > > you show the forms it look just like th user left it before it was hidden.
    > > --
    > > HTH...
    > >
    > > Jim Thomlinson
    > >
    > >
    > > "-JEFF-" wrote:
    > >
    > > > I am trying to maintain variables value so when I return to a userform that
    > > > has already been used, it will already be filled out (as long as the user has
    > > > previously visited it. I thought declaring the variables as public at the
    > > > module level would make them visible in all subs but it is not working.
    > > >
    > > > In module 3 I have variables declared as public. In the sub, the variables
    > > > are not declared because I want to use to publics but they seem to get over
    > > > written when they are assigned a value. ie.
    > > >
    > > > MODULE 3
    > > > public v1 as string
    > > > public v2 as string
    > > >
    > > > private sub MyButton_click()
    > > > v1 = "myValue"
    > > > v2 = "myValue"
    > > > end sub
    > > >
    > > > The watch window shows <Out of context> after values have been assigned
    > > > inside the sub.
    > > >


  7. #7
    -JEFF-
    Guest

    Re: UNDERSTANDING VARIABLE SCOPE

    I had't refreshed when I posted my last so I didn't see this one. I do need
    to know how to create and use public variables but what I am trying to
    accomplish here is, I need to keep the info entered into a form so the user
    can have an opportunity to return and make corrections. The catch is I need
    to be able to determine if it is the first time or if the user is returning
    to make corrections.
    -JEFF-

    "Jim Thomlinson" wrote:

    > Not how I read the question but who knows. My understanding was that the OP
    > wanted the values that last populated a form to persist until the next time
    > the form was shown. If the form is unloaded then the values are lost. This
    > being the case the OP wanted to store those values globally and then populate
    > the form with these stored values the next time the form was shown. If the
    > form is hidden instead of unloaded tehn the values are not lost and there is
    > no need to store the values globally. But is is a little unclear and I see
    > your point.
    >
    > Jeff if we have not answered your question or if you are more confused than
    > when you first asked just reply back and perhaps we can get you on track.
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Hey Jim,
    > >
    > > That shouldn't be it. It would be true if the variables were declared in the
    > > form, but if the variables are declared in a normal code module, as the OP
    > > states, their scope extends beyond the life of the form.
    > >
    > > It sounds as if the OP has also declared them as class variables in the
    > > form, and so the form uses them, but that is just a guess.
    > >
    > > --
    > >
    > > HTH
    > >
    > > RP
    > > (remove nothere from the email address if mailing direct)
    > >
    > >
    > > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > > news:7A312F4B-3374-47F6-B3E7-48A4AF22BC07@microsoft.com...
    > > > The values in the form are being discarded because you are unloading the
    > > > form. Instead of unloading the form just hide it. By doing this the values
    > > > will not be lost and you will not need to store them anywhere. The next

    > > time
    > > > you show the forms it look just like th user left it before it was hidden.
    > > > --
    > > > HTH...
    > > >
    > > > Jim Thomlinson
    > > >
    > > >
    > > > "-JEFF-" wrote:
    > > >
    > > > > I am trying to maintain variables value so when I return to a userform

    > > that
    > > > > has already been used, it will already be filled out (as long as the

    > > user has
    > > > > previously visited it. I thought declaring the variables as public at

    > > the
    > > > > module level would make them visible in all subs but it is not working.
    > > > >
    > > > > In module 3 I have variables declared as public. In the sub, the

    > > variables
    > > > > are not declared because I want to use to publics but they seem to get

    > > over
    > > > > written when they are assigned a value. ie.
    > > > >
    > > > > MODULE 3
    > > > > public v1 as string
    > > > > public v2 as string
    > > > >
    > > > > private sub MyButton_click()
    > > > > v1 = "myValue"
    > > > > v2 = "myValue"
    > > > > end sub
    > > > >
    > > > > The watch window shows <Out of context> after values have been assigned
    > > > > inside the sub.
    > > > >

    > >
    > >
    > >


  8. #8
    Bob Phillips
    Guest

    Re: UNDERSTANDING VARIABLE SCOPE

    Jeff,

    I have posted a sample workbook at http://cjoint.com/?iyxvpLS2bJ

    Download it, click the button, enter some valid date, and then click OK
    again. Then repeat. Analyse the code to see how it happens.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:333FC85E-04EA-4B9B-8B79-47E9DC631012@microsoft.com...
    > Not how I read the question but who knows. My understanding was that the

    OP
    > wanted the values that last populated a form to persist until the next

    time
    > the form was shown. If the form is unloaded then the values are lost. This
    > being the case the OP wanted to store those values globally and then

    populate
    > the form with these stored values the next time the form was shown. If the
    > form is hidden instead of unloaded tehn the values are not lost and there

    is
    > no need to store the values globally. But is is a little unclear and I see
    > your point.
    >
    > Jeff if we have not answered your question or if you are more confused

    than
    > when you first asked just reply back and perhaps we can get you on track.
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Hey Jim,
    > >
    > > That shouldn't be it. It would be true if the variables were declared in

    the
    > > form, but if the variables are declared in a normal code module, as the

    OP
    > > states, their scope extends beyond the life of the form.
    > >
    > > It sounds as if the OP has also declared them as class variables in the
    > > form, and so the form uses them, but that is just a guess.
    > >
    > > --
    > >
    > > HTH
    > >
    > > RP
    > > (remove nothere from the email address if mailing direct)
    > >
    > >
    > > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > > news:7A312F4B-3374-47F6-B3E7-48A4AF22BC07@microsoft.com...
    > > > The values in the form are being discarded because you are unloading

    the
    > > > form. Instead of unloading the form just hide it. By doing this the

    values
    > > > will not be lost and you will not need to store them anywhere. The

    next
    > > time
    > > > you show the forms it look just like th user left it before it was

    hidden.
    > > > --
    > > > HTH...
    > > >
    > > > Jim Thomlinson
    > > >
    > > >
    > > > "-JEFF-" wrote:
    > > >
    > > > > I am trying to maintain variables value so when I return to a

    userform
    > > that
    > > > > has already been used, it will already be filled out (as long as the

    > > user has
    > > > > previously visited it. I thought declaring the variables as public

    at
    > > the
    > > > > module level would make them visible in all subs but it is not

    working.
    > > > >
    > > > > In module 3 I have variables declared as public. In the sub, the

    > > variables
    > > > > are not declared because I want to use to publics but they seem to

    get
    > > over
    > > > > written when they are assigned a value. ie.
    > > > >
    > > > > MODULE 3
    > > > > public v1 as string
    > > > > public v2 as string
    > > > >
    > > > > private sub MyButton_click()
    > > > > v1 = "myValue"
    > > > > v2 = "myValue"
    > > > > end sub
    > > > >
    > > > > The watch window shows <Out of context> after values have been

    assigned
    > > > > inside the sub.
    > > > >

    > >
    > >
    > >




  9. #9
    Jim Thomlinson
    Guest

    Re: UNDERSTANDING VARIABLE SCOPE

    Forms have an initailize event which only fires when the form is first
    created and and activate event which is fired when the form is shown. Using
    these you can know if the user has been there or not.
    --
    HTH...

    Jim Thomlinson


    "-JEFF-" wrote:

    > I had't refreshed when I posted my last so I didn't see this one. I do need
    > to know how to create and use public variables but what I am trying to
    > accomplish here is, I need to keep the info entered into a form so the user
    > can have an opportunity to return and make corrections. The catch is I need
    > to be able to determine if it is the first time or if the user is returning
    > to make corrections.
    > -JEFF-
    >
    > "Jim Thomlinson" wrote:
    >
    > > Not how I read the question but who knows. My understanding was that the OP
    > > wanted the values that last populated a form to persist until the next time
    > > the form was shown. If the form is unloaded then the values are lost. This
    > > being the case the OP wanted to store those values globally and then populate
    > > the form with these stored values the next time the form was shown. If the
    > > form is hidden instead of unloaded tehn the values are not lost and there is
    > > no need to store the values globally. But is is a little unclear and I see
    > > your point.
    > >
    > > Jeff if we have not answered your question or if you are more confused than
    > > when you first asked just reply back and perhaps we can get you on track.
    > > --
    > > HTH...
    > >
    > > Jim Thomlinson
    > >
    > >
    > > "Bob Phillips" wrote:
    > >
    > > > Hey Jim,
    > > >
    > > > That shouldn't be it. It would be true if the variables were declared in the
    > > > form, but if the variables are declared in a normal code module, as the OP
    > > > states, their scope extends beyond the life of the form.
    > > >
    > > > It sounds as if the OP has also declared them as class variables in the
    > > > form, and so the form uses them, but that is just a guess.
    > > >
    > > > --
    > > >
    > > > HTH
    > > >
    > > > RP
    > > > (remove nothere from the email address if mailing direct)
    > > >
    > > >
    > > > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > > > news:7A312F4B-3374-47F6-B3E7-48A4AF22BC07@microsoft.com...
    > > > > The values in the form are being discarded because you are unloading the
    > > > > form. Instead of unloading the form just hide it. By doing this the values
    > > > > will not be lost and you will not need to store them anywhere. The next
    > > > time
    > > > > you show the forms it look just like th user left it before it was hidden.
    > > > > --
    > > > > HTH...
    > > > >
    > > > > Jim Thomlinson
    > > > >
    > > > >
    > > > > "-JEFF-" wrote:
    > > > >
    > > > > > I am trying to maintain variables value so when I return to a userform
    > > > that
    > > > > > has already been used, it will already be filled out (as long as the
    > > > user has
    > > > > > previously visited it. I thought declaring the variables as public at
    > > > the
    > > > > > module level would make them visible in all subs but it is not working.
    > > > > >
    > > > > > In module 3 I have variables declared as public. In the sub, the
    > > > variables
    > > > > > are not declared because I want to use to publics but they seem to get
    > > > over
    > > > > > written when they are assigned a value. ie.
    > > > > >
    > > > > > MODULE 3
    > > > > > public v1 as string
    > > > > > public v2 as string
    > > > > >
    > > > > > private sub MyButton_click()
    > > > > > v1 = "myValue"
    > > > > > v2 = "myValue"
    > > > > > end sub
    > > > > >
    > > > > > The watch window shows <Out of context> after values have been assigned
    > > > > > inside the sub.
    > > > > >
    > > >
    > > >
    > > >


  10. #10
    -JEFF-
    Guest

    RE: UNDERSTANDING VARIABLE SCOPE

    Thanks everybody for your help. One problem I have corrected is that I was
    trying to use input boxes on a userform and have now switched to text boxes
    which stores the data to the myBox.text. Much easier to work with.


    "-JEFF-" wrote:

    > I am trying to maintain variables value so when I return to a userform that
    > has already been used, it will already be filled out (as long as the user has
    > previously visited it. I thought declaring the variables as public at the
    > module level would make them visible in all subs but it is not working.
    >
    > In module 3 I have variables declared as public. In the sub, the variables
    > are not declared because I want to use to publics but they seem to get over
    > written when they are assigned a value. ie.
    >
    > MODULE 3
    > public v1 as string
    > public v2 as string
    >
    > private sub MyButton_click()
    > v1 = "myValue"
    > v2 = "myValue"
    > end sub
    >
    > The watch window shows <Out of context> after values have been assigned
    > inside the sub.
    >


+ 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