Persisting value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clive Moss

    #1

    Persisting value

    Anyone help a beginner at VBA please?

    I am trying to find a way to use a variable throughout an application.

    I want to take the value from a log-in form (the user) and use it in most
    forms that user opens.

    I have tried a number of things but none seem to work

    TIA

    Clive


  • Peter Doering

    #2
    Re: Persisting value

    Clive,
    [color=blue]
    > I am trying to find a way to use a variable throughout an application.[/color]
    [color=blue]
    > I want to take the value from a log-in form (the user) and use it in most
    > forms that user opens.[/color]

    You have to declare the variable in a module(!) as Public:

    Public YourUserID As String

    HTH - Peter

    --
    No mails please.

    Comment

    • Andrew Smith

      #3
      Re: Persisting value

      One way would be to leave the log in form open all the time so the value can
      always be retrieved directly from the form. If the "OK" button on the form
      hides the form rather than closing it then this should work. To simplyfy the
      code you could create a simple function that looks up the form value, and
      then just use the function name whenever you need the value.

      "Clive Moss" <clive@REMOVEin stant-image.co.uk> wrote in message
      news:ji38b.7502 $da1.65658994@n ews-text.cableinet. net...[color=blue]
      > Anyone help a beginner at VBA please?
      >
      > I am trying to find a way to use a variable throughout an application.
      >
      > I want to take the value from a log-in form (the user) and use it in most
      > forms that user opens.
      >
      > I have tried a number of things but none seem to work
      >
      > TIA
      >
      > Clive
      >
      >[/color]


      Comment

      • Clive Moss

        #4
        Re: Persisting value

        Thanks Andrew - I had tried a version of that idea but I must have done
        something else wrong because it didn't work

        I'll try it again after dinner!

        Clive

        "Andrew Smith" <andydsmith@NOS PAM.ntlworld.co m> wrote in message
        news:5n38b.486$ 1V5.20@newsfep4-winn.server.ntl i.net...[color=blue]
        > One way would be to leave the log in form open all the time so the value[/color]
        can[color=blue]
        > always be retrieved directly from the form. If the "OK" button on the form
        > hides the form rather than closing it then this should work. To simplyfy[/color]
        the[color=blue]
        > code you could create a simple function that looks up the form value, and
        > then just use the function name whenever you need the value.
        >
        > "Clive Moss" <clive@REMOVEin stant-image.co.uk> wrote in message
        > news:ji38b.7502 $da1.65658994@n ews-text.cableinet. net...[color=green]
        > > Anyone help a beginner at VBA please?
        > >
        > > I am trying to find a way to use a variable throughout an application.
        > >
        > > I want to take the value from a log-in form (the user) and use it in[/color][/color]
        most[color=blue][color=green]
        > > forms that user opens.
        > >
        > > I have tried a number of things but none seem to work
        > >
        > > TIA
        > >
        > > Clive
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Clive Moss

          #5
          Re: Persisting value

          Thanks again - Just tried it out and it works fine - I was doing something
          really stupid before which is too embarrassing and long winded to explain

          Clive

          "Clive Moss" <clive@REMOVEin stant-image.co.uk> wrote in message
          news:_548b.7585 $ir2.67203264@n ews-text.cableinet. net...[color=blue]
          > Thanks Andrew - I had tried a version of that idea but I must have done
          > something else wrong because it didn't work
          >
          > I'll try it again after dinner!
          >
          > Clive
          >
          > "Andrew Smith" <andydsmith@NOS PAM.ntlworld.co m> wrote in message
          > news:5n38b.486$ 1V5.20@newsfep4-winn.server.ntl i.net...[color=green]
          > > One way would be to leave the log in form open all the time so the value[/color]
          > can[color=green]
          > > always be retrieved directly from the form. If the "OK" button on the[/color][/color]
          form[color=blue][color=green]
          > > hides the form rather than closing it then this should work. To simplyfy[/color]
          > the[color=green]
          > > code you could create a simple function that looks up the form value,[/color][/color]
          and[color=blue][color=green]
          > > then just use the function name whenever you need the value.
          > >
          > > "Clive Moss" <clive@REMOVEin stant-image.co.uk> wrote in message
          > > news:ji38b.7502 $da1.65658994@n ews-text.cableinet. net...[color=darkred]
          > > > Anyone help a beginner at VBA please?
          > > >
          > > > I am trying to find a way to use a variable throughout an application.
          > > >
          > > > I want to take the value from a log-in form (the user) and use it in[/color][/color]
          > most[color=green][color=darkred]
          > > > forms that user opens.
          > > >
          > > > I have tried a number of things but none seem to work
          > > >
          > > > TIA
          > > >
          > > > Clive
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Michael Paul

            #6
            Re: Persisting value

            Leaving the login form open is a good method. Should you need to have
            it closed, or if a user closes it, another method is to store the
            values(s) you want to Global (Public) variables as the form closes. I
            use this method for exactly the same reason, to store login information
            and it has worked well with all my apps. Another alternative is to
            write the value(s) to a temp file (.txt) and read the file when needed.
            Look for and delete the file at application start up as well as at
            application close to ensure the file is created at each application
            start.

            MPP
            Lanexa, Virginia

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Pieter Linden

              #7
              Re: Persisting value

              I like Peter's idea. If you have to persist it across sessions
              (open/close of database) then you should look into writing it to
              either a table or a custom database property. Then retrieve it when
              the database opens like Peter said.

              HTH,
              Village Idiot

              Comment

              • John Mishefske

                #8
                Re: Persisting value

                Clive Moss wrote:[color=blue]
                > Thanks again - Just tried it out and it works fine - I was doing something
                > really stupid before which is too embarrassing and long winded to explain
                >
                > Clive[/color]

                Unhandled errors (or more exactly - the user resetting code execution) will
                reset (or clear) global variables. Perhaps that is what happened to you?
                Make sure you have error handlers in your code procedures.

                Save the value in a table if you need persistence or consider classes to
                prevent this type of problem.

                --
                '-------------------------------
                ' John Mishefske
                '-------------------------------

                Comment

                • Tony Toews

                  #9
                  Re: Persisting value

                  Peter Doering <news@doering.o rg> wrote:
                  [color=blue]
                  >You have to declare the variable in a module(!) as Public:
                  >
                  >Public YourUserID As String[/color]

                  Trouble is if you're in the middle of running some code, hit a breakpoint or
                  whatever, and then stop the code you lose all these variables.

                  Thus I use an always open hidden form to store such variables in.

                  Tony
                  --
                  Tony Toews, Microsoft Access MVP
                  Please respond only in the newsgroups so that others can
                  read the entire thread of messages.
                  Microsoft Access Links, Hints, Tips & Accounting Systems at

                  Comment

                  • Peter Doering

                    #10
                    Re: Persisting value

                    Tony,
                    [color=blue][color=green]
                    >>Public YourUserID As String[/color][/color]
                    [color=blue]
                    > Trouble is if you're in the middle of running some code, hit a breakpoint or
                    > whatever, and then stop the code you lose all these variables.[/color]

                    Sure, that's what I want. I see it as a feature, not a bug. What's wrong
                    with the user having to login again after a program failure?

                    Your statement is true also for all other global variables (which should be
                    avoided in general, I know). Perfect for storage of passwords, aren't they.
                    [color=blue]
                    > Thus I use an always open hidden form to store such variables in.[/color]

                    There's no argument against this method either (unless one prefers global
                    variables ;-)

                    Peter

                    --
                    No mails please.

                    Comment

                    Working...