Referencing a public variable in MS Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sue999
    New Member
    • Feb 2016
    • 6

    Referencing a public variable in MS Access

    I'm trying to reference a public variable that I have declared in a form module but the user module that is calling it doesn't recognise it. I haven't used VBA for some years and have forgotten how to do it! The MS help file tells how to declare a public variable but not how to reference it. The declaration is [Public strCurrentUser as String]. Can anyone remind me how to reference it, please? Thanks.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1288

    #2
    There is no trick to referencing a public variable, simple reference it by name. But if you declared it public in a form then the form must be open when the public variable is used by some module.

    You say "the user module that is calling it doesn't recognise it."
    I don't understand that. Is the sub or function referencing the variable being called by some code in the form where the public variable is defined?

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      It's great that you included the declaration Sue. It would also help to include your attempt to reference it, with particular attention to whether that's from VBA or SQL.

      Comment

      • Sue999
        New Member
        • Feb 2016
        • 6

        #4
        The trick was knowing the form had to be referenced as well. and I was using a SQL statement within VBA. Thanks for the responses: I actually got the answer straight away from the useful items that popped up below my post.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          A Public Variable declared within the General Declarations section of a Form essentially becomes a Property of the Form and must be referenced accordingly, as in:

          FormName.Variab leName

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            Originally posted by Sue
            Sue:
            The trick was knowing the form had to be referenced as well.
            The more important trick is to include as much information as is relevant in the question. We know many of the answers, but can only give those that match what's in the question.

            I'm not trying to nag, but if you take this point on board you'll get much more benefit than the answer to your current problem. I'm very pleased you managed to get that anyway :-)

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              It may also be worth mentioning which bytes.com thread led you to the answer. This will help anyone in the future that may be searching using the same verbiage that you used. :)

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                I'm not sure I follow Luk3r. I believe it's clear that the post shown as Best Answer was what led to the understanding. It wasn't a separate thread.

                Comment

                • mbizup
                  New Member
                  • Jun 2015
                  • 80

                  #9
                  As an aside, Public/Global variables are often (more commonly?) declared in separate public modules rather than in the code module behind forms. If you declare your variables in separate modules (such as one named modGlobals), you do not need the form reference as a prefix as mentioned in the top post.

                  Also, if you are using Access 2007 or later with the .accdb format, TempVars are a pretty powerful alternative to Global Variables. You can define them anywhere in your code, and use them anywhere else in your database - forms, code, reports, even property sheets of controls. One of the advantages of TempVars over Globals is that their values persist until the database is closed, whereas Globals are lost when runtime errors occur.

                  More info here if you are interested:
                  TempVars

                  Comment

                  • Luk3r
                    Contributor
                    • Jan 2014
                    • 300

                    #10
                    NeoPa, and I quote Sue999: "Thanks for the responses: I actually got the answer straight away from the useful items that popped up below my post. ".

                    It very well could be that I'm reading the response incorrectly, but to me, that implies that Sue999 got the answer from the "Similar topics" section after posting the question, because similar topics were suggested and one of them had the perfect answer. Sorry if this cause(s/ed) confusion.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      Hi Luk3r.

                      Your interpretation is probably right. I wasn't sure what she meant by that as it's not clear what she's talking about. I assumed she was referring to the responses, but your interpretation is just as likely, if not more so.

                      As far as this question goes though, I'm happy that the question has been answered clearly in the post selected as best answer. Also, further important understandings around the subject have also been shared by various experts so we have a good result.

                      Comment

                      • Sue999
                        New Member
                        • Feb 2016
                        • 6

                        #12
                        Great, thanks! I shall explore this.

                        Comment

                        Working...