By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,834 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,834 IT Pros & Developers. It's quick & easy.

General Global Variable Help

blyxx86
100+
P: 256
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.
Jun 1 '07 #1
Share this Question
Share on Google+
13 Replies

ADezii
Expert 5K+
P: 8,750
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.
Expand|Select|Wrap|Line Numbers
  1. Public gintMyID As Integer
  2. Public gstrCompanyName As String
  3. Public gcurAnnualSalary As Currency
  4. Public glngSomeBigWholeNumber As Long
  5. Public gdblValueOfPI As Double
  6. Public gblnFileFound As Boolean
  7. Public gdteDateCreated As Date
...I think you get the idea...
Jun 2 '07 #2

maxamis4
Expert 100+
P: 295
I understand global variables, but how do you have them retain their value througout the application, case and point a session?
Jun 2 '07 #3

ADezii
Expert 5K+
P: 8,750
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.
    Expand|Select|Wrap|Line Numbers
    1. Public gstrFilePath As String
  2. Initialize it at some point, here in the Main Form's Open Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Open(Cancel As Integer)
    2.   gstrFilePath = "C:\Test Directory\"
    3. End Sub
  3. Reference it at anytime during the lifetime of your Application.
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print gstrFilePath 
    2. produces ==> C:\Test Directory\
  4. I hope this explained everything, if not, please feel free to ask.
Jun 2 '07 #4

MSeda
Expert 100+
P: 159
a global object variable can be used to make a single data entry or search form work with any form.

in module:

Expand|Select|Wrap|Line Numbers
  1. Global myObj as object
  2.  
in form module for command button that opens search form:
Expand|Select|Wrap|Line Numbers
  1.  
  2. private sub cmdfind_click()
  3.  
  4. set myobj  = me.text0
  5. docmd.openform "Search Form", , , , , acDialog
  6. text0_afterupdate 
  7.  
  8. End sub
  9.  
in form module for search command button that enters data to other forms:

Expand|Select|Wrap|Line Numbers
  1. private sub cmdenter_click()
  2.  
  3. myObj = me.ID
  4. docmd.close acform, me.form.name
  5.  
  6. End sub
  7.  
Jun 2 '07 #5

ADezii
Expert 5K+
P: 8,750
a global object variable can be used to make a single data entry or search form work with any form.

in module:

Expand|Select|Wrap|Line Numbers
  1. Global myObj as object
  2.  
in form module for command button that opens search form:
Expand|Select|Wrap|Line Numbers
  1.  
  2. private sub cmdfind_click()
  3.  
  4. set myobj  = me.text0
  5. docmd.openform "Search Form", , , , , acDialog
  6. text0_afterupdate 
  7.  
  8. End sub
  9.  
in form module for search command button that enters data to other forms:

Expand|Select|Wrap|Line Numbers
  1. private sub cmdenter_click()
  2.  
  3. myObj = me.ID
  4. docmd.close acform, me.form.name
  5.  
  6. End sub
  7.  
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 ------------------------
Expand|Select|Wrap|Line Numbers
  1. Dim objText As Textbox
  2. Set objText = Forms!Form1![txtTest]
------------------------ NOT ------------------------
Expand|Select|Wrap|Line Numbers
  1. Dim objText As Object
  2. Set objText = Forms!Form1![txtTest]
-------------------------------- ------------------------
Jun 2 '07 #6

blyxx86
100+
P: 256
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 (frmFindLocation) that is used to 'Find' addresses from a table that contains the addresses (tblLocations) and insert the found value (tblLocations.LocationID) 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 gstrLocationForm = 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?
Jun 3 '07 #7

ADezii
Expert 5K+
P: 8,750
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 (frmFindLocation) that is used to 'Find' addresses from a table that contains the addresses (tblLocations) and insert the found value (tblLocations.LocationID) 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 gstrLocationForm = 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:
Expand|Select|Wrap|Line Numbers
  1.  'to set the value in frmFindLocation
  2. glngLocationID = Me![LocationID]
  3.  
  4. 'to return the value of LocationID to another Form
  5. Forms!frmShipment![LocationID] = glngLocationID
Jun 4 '07 #8

blyxx86
100+
P: 256
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:
Expand|Select|Wrap|Line Numbers
  1.  'to set the value in frmFindLocation
  2. glngLocationID = Me![LocationID]
  3.  
  4. 'to return the value of LocationID to another Form
  5. Forms!frmShipment![LocationID] = glngLocationID
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...

Expand|Select|Wrap|Line Numbers
  1.  'Set on cmdOpenFindLocation from within each form. Is there a way to return the name of a form???
  2. gstrFindLocationParent= frmShipment
  3.  
  4. 'Then using your method it would look like this in the frmFindLocation form
  5. 'to set the value in frmFindLocation
  6. glngLocationID = Me![LocationID]
  7.  
  8. 'to return the value of LocationID to another Form
  9. Forms! & gstrFindLocationParent & ![LocationID] = glngLocationID
Would that work???
Jun 4 '07 #9

ADezii
Expert 5K+
P: 8,750
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...

Expand|Select|Wrap|Line Numbers
  1.  'Set on cmdOpenFindLocation from within each form. Is there a way to return the name of a form???
  2. gstrFindLocationParent= frmShipment
  3.  
  4. 'Then using your method it would look like this in the frmFindLocation form
  5. 'to set the value in frmFindLocation
  6. glngLocationID = Me![LocationID]
  7.  
  8. 'to return the value of LocationID to another Form
  9. Forms! & gstrFindLocationParent & ![LocationID] = glngLocationID
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.
    Expand|Select|Wrap|Line Numbers
    1. 'This code can be in the Click() Event of a Command Button on frmEmployees (1st Form)
    2. DoCmd.OpenForm "frmOLE", acNormal, , , acFormEdit, acWindowNormal, "frmEmployees"
  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.
    Expand|Select|Wrap|Line Numbers
    1. Select Case Me.OpenArgs
    2.   Case "frmEmployees"
    3.     'Global Variable would be returned to the [LocationID] Field of frmEmployees
    4.     Forms!frmEmployees![LocationID] = gGlobal_Variable
    5.   Case "frmTwo"
    6.     Forms!frmTwo![LocationID] = gGlobal_Variable
    7.   Case "frmThree"
    8.     Forms!frmThree![LocationID] = gGlobal_Variable
    9.   Case "frmFour"
    10.     Forms!frmFour![LocationID] = gGlobal_Variable
    11.   Case Else
    12.     'do nothing
    13. End Select
  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.
Jun 5 '07 #10

blyxx86
100+
P: 256
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.
    Expand|Select|Wrap|Line Numbers
    1. 'This code can be in the Click() Event of a Command Button on frmEmployees (1st Form)
    2. DoCmd.OpenForm "frmOLE", acNormal, , , acFormEdit, acWindowNormal, "frmEmployees"
  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.
    Expand|Select|Wrap|Line Numbers
    1. Select Case Me.OpenArgs
    2.   Case "frmEmployees"
    3.     'Global Variable would be returned to the [LocationID] Field of frmEmployees
    4.     Forms!frmEmployees![LocationID] = gGlobal_Variable
    5.   Case "frmTwo"
    6.     Forms!frmTwo![LocationID] = gGlobal_Variable
    7.   Case "frmThree"
    8.     Forms!frmThree![LocationID] = gGlobal_Variable
    9.   Case "frmFour"
    10.     Forms!frmFour![LocationID] = gGlobal_Variable
    11.   Case Else
    12.     'do nothing
    13. End Select
  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
Expand|Select|Wrap|Line Numbers
  1.  DoCmd.OpenForm "frmFindLocation", acNormal, , , acFormEdit, acWindowNormal, "frmRequest" 'Or any of the other forms that open the frmFindLocation form.
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..
Expand|Select|Wrap|Line Numbers
  1. glngLocationID = Me![LocationID]
  2. Select Case Me.OpenArgs
  3.   Case "frmRequest"
  4.     'Global Variable would be returned to the [LocationID] Field of frmEmployees
  5.     Forms!frmRequest![LocationID] = glngLocationID
  6.   Case "frmReceiving"
  7.     Forms!frmReceiving![LocationID] = glngLocationID
  8.   Case "frmShipping"
  9.     Forms!frmShipping![LocationID] = glngLocationID
  10.   Case Else
  11.     'do nothing
  12. End Select
Do I have this correct? In general? This is my first venture into global variables and digging past simple 'OpenForm' commands in visual basic.
Jun 5 '07 #11

ADezii
Expert 5K+
P: 8,750
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
Expand|Select|Wrap|Line Numbers
  1.  DoCmd.OpenForm "frmFindLocation", acNormal, , , acFormEdit, acWindowNormal, "frmRequest" 'Or any of the other forms that open the frmFindLocation form.
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..
Expand|Select|Wrap|Line Numbers
  1. glngLocationID = Me![LocationID]
  2. Select Case Me.OpenArgs
  3.   Case "frmRequest"
  4.     'Global Variable would be returned to the [LocationID] Field of frmEmployees
  5.     Forms!frmRequest![LocationID] = glngLocationID
  6.   Case "frmReceiving"
  7.     Forms!frmReceiving![LocationID] = glngLocationID
  8.   Case "frmShipping"
  9.     Forms!frmShipping![LocationID] = glngLocationID
  10.   Case Else
  11.     'do nothing
  12. End Select
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.
Jun 5 '07 #12

blyxx86
100+
P: 256
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.
Jun 5 '07 #13

ADezii
Expert 5K+
P: 8,750
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.
Jun 5 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.