473,402 Members | 2,061 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,402 software developers and data experts.

How to set up a global variable to use within various modules?

21
I apologize in advance - I know this is a basic question but I absolutely can't find a way for this to work.

Currently I have a database I've made, with several processes of importing files, modifying the results, re-exporting files, etc. These are all done as part of a nearly-automated monthly process.

Most of these monthly processes pull from folder and file names with the numeric month in the name. So currently, I begin each function with:

Expand|Select|Wrap|Line Numbers
  1. Dim Month As String
  2. Month = "09"
  3.  
Then I use variable Month within various string command texts (TransferText, RunSQL, etc.). For example, making a new table for the month:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.CopyObject , "Results_" & Month & "2010", acTable, "Results_EMPTY"
  2.  
I know there's a way to set up one global variable called glbMonth, in one module, that I call to in all of these other modules which I currently begin with the 2 lines above. However, I haven't gotten any of these to work.

How do I set up this global variable module? More importantly, how do I call this variable within these functions, so I can continue to use it the way I have (as text)?

Thanks for your help!!


P.S. This is my own personal database to make a monthly process automated, and having read the cons of global variables, I believe it will work for me in this minor use case.
Nov 5 '10 #1
8 39596
ADezii
8,834 Expert 8TB
First of all, it is never a good idea to Name a Variable Month since it is an Intrinsic Access Function (Month()). That being said, to Declare a Variable Globally, you must Declare it in a Standard Code Module as Public:
Expand|Select|Wrap|Line Numbers
  1. Public gstrMonth As String
where g denotes its Scope (Global/Public) and str (String) its Data Type.

Once the Varibale is initialized, you may use it as you previously indicated:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.CopyObject , "Results_" & gstrMonth & "2010", acTable, "Results_EMPTY" 
It will be 'Visible' anywhere within you Application, since it is Public.
Nov 5 '10 #2
CarrieR
21
That sets it as a variable, but I need to define it, so it is defined the same way in every module that calls it (that is, setting it = "10", for example).

So I wrote:
Expand|Select|Wrap|Line Numbers
  1. Public gstrMonth As String
  2.         gstrMonth = "10" 
in a module SetGlobals. In one of the many modules that reference it, I simply used "gstrMonth" instead of "Month" as you said. This is exactly where I have gotten tripped up before.

Error message is "Compile error: Invalid Outside Procedure." It marks this at the "10" in the Global Variables Modules.

So my question is, how can I get this to work so not only is the global var created, but it is set to "10" (this month) in ONE place, and all of the modules that call it now know that it = "10"?
Nov 8 '10 #3
CarrieR
21
I ended up solving my issue through a lot of trial and error. Here was my solution:

I set up new module SetGlobalVariables with the following code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Public gstrMonth As String
  4.  
  5. Public Function SetMonthValue()
  6. gstrMonth = "10"
  7. End Function 
Then within one of the many modules that needed to call this variable, where I used to have:

Expand|Select|Wrap|Line Numbers
  1. Dim Month As String
  2. Month = "10" 
I replaced it with:

Expand|Select|Wrap|Line Numbers
  1. Call SetMonthValue 
This did what I needed, and solved my problem.
Nov 8 '10 #4
ADezii
8,834 Expert 8TB
There is no need to Call a Function in order to Define a Public Variable. Since it is Public/Global, you can Initialize/Define it anywhere within your Application. A good place may be the Open() Event of your Main Form, as in:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.   gstrMonth = "10"
  3. End Sub
Nov 8 '10 #5
CarrieR
21
But I actually need to define it globally. (For the purposes of this DB that only I use, this is what I need.)

I don't use any forms. This is all within modules.
Nov 8 '10 #6
ADezii
8,834 Expert 8TB
You can then create an AutoExec Mcro that Calls the SetMonthValue() Function, where gstrMonth is initialized.
Nov 8 '10 #7
CarrieR
21
Thanks, but unless there's some reason I shouldn't be doing it the way that I did, that works perfectly for what I needed.

If there is a reason, let me know, but otherwise, it works well!
Nov 8 '10 #8
ADezii
8,834 Expert 8TB
If it ain't broke, then don't fix it! (LOL).
Nov 8 '10 #9

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

Similar topics

7
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
2
by: TNGgroup | last post by:
Hi, a Simple Question, I have some code at my form, the code in the form is calling a certain module and execute some code, a certain variable is set to strTemp. but when de module is done. I...
13
by: Le, Thanh-Nhan | last post by:
Hi, How can I define a global variable in .NET? I mean, everywhere in the same project can access this variable. Thanks Nhan
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
9
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
3
by: ankugoe7 | last post by:
Is it possible to share global variables such that all applications which use shared libraries can see the changes made to the global variables by the other applications.
2
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a...
14
by: Fred Block | last post by:
Hi All, I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and I'm very excited. Here's an issue I'm experiencing right off the starting line and cannot make sense of it:...
1
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
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: 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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.