473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

General Global Variable Help

blyxx86
256 100+
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
13 2859
ADezii
8,834 Expert 8TB
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
295 Expert 100+
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
8,834 Expert 8TB
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
159 Expert 100+
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
8,834 Expert 8TB
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
256 100+
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
8,834 Expert 8TB
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
256 100+
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
8,834 Expert 8TB
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
256 100+
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
8,834 Expert 8TB
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
256 100+
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
8,834 Expert 8TB
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

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

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
9
by: ruca | last post by:
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET application? Example: var aux=null; function...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
25
by: lovecreatesbeauty | last post by:
Could you talk something about the general rules on the interface (function) design in C program that recognized publically? Or introduce some good advice of yourself. How do you think about...
8
by: yinglcs | last post by:
Hi, I read this article about global variable in c: http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html But I have a few questions 1. how can I declare the global variable...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.