General Global Variable Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    General Global Variable Help

    Hello everyone,

    I am interested in creating an access MDE for a front-end.. In attempting to create reports and generalized forms, I've run into a problem. There are certain fields/values that I need to use, depending on what the end-user would be doing on a field...

    I guess I'm just looking for help setting up global variables, for example...

    I need the user to be able to research a location by address, on multiple fields, and I can easily create a button to open up a 'Location Inquiry' form, which they can search by zip code, street, city, etc.. However, I want them to be able to click a button and for it to enter the ID of the field they found and enter it into the previous form...

    I can do that with absolute paths, but as I said, there are many forms that need the ID for the location, but I do not want to have to create the form over and over for each different form.

    This is one specific case, and I have many different forms that require similar globalized variables.. Does anyone have any suggestions or know of any sites that can guide me in the right direction? This site has guided me rather far.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by blyxx86
    Hello everyone,

    I am interested in creating an access MDE for a front-end.. In attempting to create reports and generalized forms, I've run into a problem. There are certain fields/values that I need to use, depending on what the end-user would be doing on a field...

    I guess I'm just looking for help setting up global variables, for example...

    I need the user to be able to research a location by address, on multiple fields, and I can easily create a button to open up a 'Location Inquiry' form, which they can search by zip code, street, city, etc.. However, I want them to be able to click a button and for it to enter the ID of the field they found and enter it into the previous form...

    I can do that with absolute paths, but as I said, there are many forms that need the ID for the location, but I do not want to have to create the form over and over for each different form.

    This is one specific case, and I have many different forms that require similar globalized variables.. Does anyone have any suggestions or know of any sites that can guide me in the right direction? This site has guided me rather far.
    It's a lot simpler than you think:
    1. To make a variable 'Global' Declare it as Public in a Standard Code Module along with its associated Data Type.
    2. Keep the Standard Naming Convention of prefacing the Global Variable with g, then the Data Type of the Variable.
    3. I've included several examples below.
    4. If you have any questions or problems, please let us know.
    5. NOTE! Because these Variables are 'Global', they can now be inadvertently accessed and/or modified anywhere within your application.

    [CODE=vb]Public gintMyID As Integer
    Public gstrCompanyName As String
    Public gcurAnnualSalar y As Currency
    Public glngSomeBigWhol eNumber As Long
    Public gdblValueOfPI As Double
    Public gblnFileFound As Boolean
    Public gdteDateCreated As Date[/CODE]
    ...I think you get the idea...

    Comment

    • maxamis4
      Recognized Expert Contributor
      • Jan 2007
      • 295

      #3
      I understand global variables, but how do you have them retain their value througout the application, case and point a session?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by maxamis4
        I understand global variables, but how do you have them retain their value througout the application, case and point a session?
        From the moment that they are initialized, until the closing of your Application, they will retain their value (persist) and can be referenced. Case in point, a Global Variable referencing an Absolute Path:
        1. Declare the Global Variable.
          [CODE=vb]Public gstrFilePath As String[/CODE]
        2. Initialize it at some point, here in the Main Form's Open Event.
          [CODE=vb]Private Sub Form_Open(Cance l As Integer)
          gstrFilePath = "C:\Test Directory\"
          End Sub[/CODE]
        3. Reference it at anytime during the lifetime of your Application.
          [CODE=vb]Debug.Print gstrFilePath
          produces ==> C:\Test Directory\[/CODE]
        4. I hope this explained everything, if not, please feel free to ask.

        Comment

        • MSeda
          Recognized Expert New Member
          • Sep 2006
          • 159

          #5
          a global object variable can be used to make a single data entry or search form work with any form.

          in module:

          Code:
          Global myObj as object
          in form module for command button that opens search form:
          Code:
          private sub cmdfind_click()
          
          set myobj  = me.text0
          docmd.openform "Search Form", , , , , acDialog
          text0_afterupdate 
          
          End sub
          in form module for search command button that enters data to other forms:

          Code:
          private sub cmdenter_click()
          
          myObj = me.ID
          docmd.close acform, me.form.name
          
          End sub

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by MSeda
            a global object variable can be used to make a single data entry or search form work with any form.

            in module:

            Code:
            Global myObj as object
            in form module for command button that opens search form:
            Code:
            private sub cmdfind_click()
            
            set myobj  = me.text0
            docmd.openform "Search Form", , , , , acDialog
            text0_afterupdate 
            
            End sub
            in form module for search command button that enters data to other forms:

            Code:
            private sub cmdenter_click()
            
            myObj = me.ID
            docmd.close acform, me.form.name
            
            End sub
            To expand on your point - Declare your Variables, whether they be Local, Modular, or Global, with the tightest Scope possible. For instance, if you have a Text Box on Form1 named txtTest, and you wish to refer to it with an Object Variable:

            ------------------------ USE ------------------------
            [CODE=VB]Dim objText As Textbox
            Set objText = Forms!Form1![txtTest][/CODE]
            ------------------------ NOT ------------------------
            [CODE=VB]Dim objText As Object
            Set objText = Forms!Form1![txtTest][/CODE]
            -------------------------------- ------------------------

            Comment

            • blyxx86
              Contributor
              • Nov 2006
              • 258

              #7
              As much as can be said.. this sounds fairly simple... However, I can not seem to make this one aspect work for me, or I can not figure out how to think about it logically...

              What I'm aiming for...
              To create a form (frmFindLocatio n) that is used to 'Find' addresses from a table that contains the addresses (tblLocations) and insert the found value (tblLocations.L ocationID) into a few seperate forms (frmShipment, frmReceive, frmRequest)...

              What I'm thinking...
              Create a command button that will open the frmFindLocation form on each of the originating forms (frmShipment, frmReceive, frmRequest) that update the global variable for the path to the LocationID on each form...
              Then... the frmFindLocation form opens, the user finds their location and presses a single button that would then return the value of their found location back to the originating form (frmShipment, frmReceive, frmRequest) and close the frmFindLocation form.

              My troubles...
              I'm not sure how to write the above said idea out in a program.


              I was tinkering with things like: Set gstrLocationFor m = frmShipment and then I ran into problems setting the value of the form to the found value... I can't think of it off the top of my head...

              Any thoughts?

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by blyxx86
                As much as can be said.. this sounds fairly simple... However, I can not seem to make this one aspect work for me, or I can not figure out how to think about it logically...

                What I'm aiming for...
                To create a form (frmFindLocatio n) that is used to 'Find' addresses from a table that contains the addresses (tblLocations) and insert the found value (tblLocations.L ocationID) into a few seperate forms (frmShipment, frmReceive, frmRequest)...

                What I'm thinking...
                Create a command button that will open the frmFindLocation form on each of the originating forms (frmShipment, frmReceive, frmRequest) that update the global variable for the path to the LocationID on each form...
                Then... the frmFindLocation form opens, the user finds their location and presses a single button that would then return the value of their found location back to the originating form (frmShipment, frmReceive, frmRequest) and close the frmFindLocation form.

                My troubles...
                I'm not sure how to write the above said idea out in a program.


                I was tinkering with things like: Set gstrLocationFor m = frmShipment and then I ran into problems setting the value of the form to the found value... I can't think of it off the top of my head...

                Any thoughts?
                Assign the value of LocationID on the frmFindLocation Form to the Global Variable. Now, it can be used anywhere in your Application unless it is modified or the Application is closed. Assuming LocationID is a Number:
                [CODE=vb] 'to set the value in frmFindLocation
                glngLocationID = Me![LocationID]

                'to return the value of LocationID to another Form
                Forms!frmShipme nt![LocationID] = glngLocationID[/CODE]

                Comment

                • blyxx86
                  Contributor
                  • Nov 2006
                  • 258

                  #9
                  Originally posted by ADezii
                  Assign the value of LocationID on the frmFindLocation Form to the Global Variable. Now, it can be used anywhere in your Application unless it is modified or the Application is closed. Assuming LocationID is a Number:
                  [CODE=vb] 'to set the value in frmFindLocation
                  glngLocationID = Me![LocationID]

                  'to return the value of LocationID to another Form
                  Forms!frmShipme nt![LocationID] = glngLocationID[/CODE]
                  Much smarter and much simpler.

                  I still have one problem.. Having the frmFindLocation form only return the value to the form it was opened from...

                  [code=vb] 'Set on cmdOpenFindLoca tion from within each form. Is there a way to return the name of a form???
                  gstrFindLocatio nParent= frmShipment

                  'Then using your method it would look like this in the frmFindLocation form
                  'to set the value in frmFindLocation
                  glngLocationID = Me![LocationID]

                  'to return the value of LocationID to another Form
                  Forms! & gstrFindLocatio nParent & ![LocationID] = glngLocationID[/code]

                  Would that work???

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Originally posted by blyxx86
                    Much smarter and much simpler.

                    I still have one problem.. Having the frmFindLocation form only return the value to the form it was opened from...

                    [code=vb] 'Set on cmdOpenFindLoca tion from within each form. Is there a way to return the name of a form???
                    gstrFindLocatio nParent= frmShipment

                    'Then using your method it would look like this in the frmFindLocation form
                    'to set the value in frmFindLocation
                    glngLocationID = Me![LocationID]

                    'to return the value of LocationID to another Form
                    Forms! & gstrFindLocatio nParent & ![LocationID] = glngLocationID[/code]

                    Would that work???
                    You're approach would never work, don't waste your time on it.
                    1. To find out what Form called a 2nd Form , you can use the OpenArgs Property. A code demo will illustrate this point in which the OpenArgs Property is the last Argument on the DoCmd.OpenForm line and is frmEmployees.
                      [CODE=vb]
                      'This code can be in the Click() Event of a Command Button on frmEmployees (1st Form)
                      DoCmd.OpenForm "frmOLE", acNormal, , , acFormEdit, acWindowNormal, "frmEmploye es"[/CODE]
                    2. At some point during the lifetime of the 2nd Form, when you want to determine which Form opened the current Form (2nd Form), you can write code similar to this. This code examines the OpenArgs Propwerty of the 2nd Form which in this case is frmEmployees.
                      [CODE=vb]Select Case Me.OpenArgs
                      Case "frmEmploye es"
                      'Global Variable would be returned to the [LocationID] Field of frmEmployees
                      Forms!frmEmploy ees![LocationID] = gGlobal_Variabl e
                      Case "frmTwo"
                      Forms!frmTwo![LocationID] = gGlobal_Variabl e
                      Case "frmThree"
                      Forms!frmThree![LocationID] = gGlobal_Variabl e
                      Case "frmFour"
                      Forms!frmFour![LocationID] = gGlobal_Variabl e
                      Case Else
                      'do nothing
                      End Select[/CODE]
                    3. I apologize for the confusion but for some reason I am having difficulty explaining this approach. Perhaps someone will have a better approach, or explain this one better.

                    Comment

                    • blyxx86
                      Contributor
                      • Nov 2006
                      • 258

                      #11
                      Originally posted by ADezii
                      You're approach would never work, don't waste your time on it.
                      1. To find out what Form called a 2nd Form , you can use the OpenArgs Property. A code demo will illustrate this point in which the OpenArgs Property is the last Argument on the DoCmd.OpenForm line and is frmEmployees.
                        [CODE=vb]
                        'This code can be in the Click() Event of a Command Button on frmEmployees (1st Form)
                        DoCmd.OpenForm "frmOLE", acNormal, , , acFormEdit, acWindowNormal, "frmEmploye es"[/CODE]
                      2. At some point during the lifetime of the 2nd Form, when you want to determine which Form opened the current Form (2nd Form), you can write code similar to this. This code examines the OpenArgs Propwerty of the 2nd Form which in this case is frmEmployees.
                        [CODE=vb]Select Case Me.OpenArgs
                        Case "frmEmploye es"
                        'Global Variable would be returned to the [LocationID] Field of frmEmployees
                        Forms!frmEmploy ees![LocationID] = gGlobal_Variabl e
                        Case "frmTwo"
                        Forms!frmTwo![LocationID] = gGlobal_Variabl e
                        Case "frmThree"
                        Forms!frmThree![LocationID] = gGlobal_Variabl e
                        Case "frmFour"
                        Forms!frmFour![LocationID] = gGlobal_Variabl e
                        Case Else
                        'do nothing
                        End Select[/CODE]
                      3. I apologize for the confusion but for some reason I am having difficulty explaining this approach. Perhaps someone will have a better approach, or explain this one better.
                      So... to attempt to understand, this is what I am seeing...
                      in any form (frmRequest, frmShipping, frmReceiving) that will open the frmFindLocation form I set a command button to follow these instructions
                      [code=vb] DoCmd.OpenForm "frmFindLocatio n", acNormal, , , acFormEdit, acWindowNormal, "frmRequest " 'Or any of the other forms that open the frmFindLocation form.[/code]
                      I would then place the following code on the cmdButton that will set the LocationID value based on the form used to open the form... This is found in the frmFindLocation form..
                      [code=vb]glngLocationID = Me![LocationID]
                      Select Case Me.OpenArgs
                      Case "frmRequest "
                      'Global Variable would be returned to the [LocationID] Field of frmEmployees
                      Forms!frmReques t![LocationID] = glngLocationID
                      Case "frmReceivi ng"
                      Forms!frmReceiv ing![LocationID] = glngLocationID
                      Case "frmShippin g"
                      Forms!frmShippi ng![LocationID] = glngLocationID
                      Case Else
                      'do nothing
                      End Select[/code]

                      Do I have this correct? In general? This is my first venture into global variables and digging past simple 'OpenForm' commands in visual basic.

                      Comment

                      • ADezii
                        Recognized Expert Expert
                        • Apr 2006
                        • 8834

                        #12
                        Originally posted by blyxx86
                        So... to attempt to understand, this is what I am seeing...
                        in any form (frmRequest, frmShipping, frmReceiving) that will open the frmFindLocation form I set a command button to follow these instructions
                        [code=vb] DoCmd.OpenForm "frmFindLocatio n", acNormal, , , acFormEdit, acWindowNormal, "frmRequest " 'Or any of the other forms that open the frmFindLocation form.[/code]
                        I would then place the following code on the cmdButton that will set the LocationID value based on the form used to open the form... This is found in the frmFindLocation form..
                        [code=vb]glngLocationID = Me![LocationID]
                        Select Case Me.OpenArgs
                        Case "frmRequest "
                        'Global Variable would be returned to the [LocationID] Field of frmEmployees
                        Forms!frmReques t![LocationID] = glngLocationID
                        Case "frmReceivi ng"
                        Forms!frmReceiv ing![LocationID] = glngLocationID
                        Case "frmShippin g"
                        Forms!frmShippi ng![LocationID] = glngLocationID
                        Case Else
                        'do nothing
                        End Select[/code]

                        Do I have this correct? In general? This is my first venture into global variables and digging past simple 'OpenForm' commands in visual basic.
                        By jove, I think you got it! Remember though, at the instant you assign [LocationID] to the Global Variable (glngLocationID = Me![LocationID]), it can now be accessed and/or modified from anywhere within you Application until you close it. Let me know how you make out.

                        Comment

                        • blyxx86
                          Contributor
                          • Nov 2006
                          • 258

                          #13
                          Originally posted by ADezii
                          By jove, I think you got it! Remember though, at the instant you assign [LocationID] to the Global Variable (glngLocationID = Me![LocationID]), it can now be accessed and/or modified from anywhere within you Application until you close it. Let me know how you make out.

                          Now, with your experience in this, would it make sense to close the frmFindLocation form after they have updated the other form, and have them reopen the form if they need to find another location? Since this will all be in an MDE, the speed shouldn't be affected too much. There is only a list of about 3500 different locations that would be queried.

                          I'll have to address my other questions regarding ways to speed up service of an access program through the network in a different post and search the forum for some more information.

                          Comment

                          • ADezii
                            Recognized Expert Expert
                            • Apr 2006
                            • 8834

                            #14
                            Originally posted by blyxx86
                            Now, with your experience in this, would it make sense to close the frmFindLocation form after they have updated the other form, and have them reopen the form if they need to find another location? Since this will all be in an MDE, the speed shouldn't be affected too much. There is only a list of about 3500 different locations that would be queried.

                            I'll have to address my other questions regarding ways to speed up service of an access program through the network in a different post and search the forum for some more information.
                            I would say that it had to depend on the frequency with which tney would be requesting new Locations. Don't forget that each opening of the Form has top pull the Record Source for the Form as well as Query 3,500 locations. Only experimentation will tell you the correct course of action.

                            Comment

                            Working...