469,366 Members | 2,251 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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 39083
ADezii
8,800 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,800 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,800 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,800 Expert 8TB
If it ain't broke, then don't fix it! (LOL).
Nov 8 '10 #9

Post your reply

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

Similar topics

2 posts views Thread by TNGgroup | last post: by
13 posts views Thread by Le, Thanh-Nhan | last post: by
24 posts views Thread by LP | last post: by
9 posts views Thread by Shilpa | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by ankugoe7 | last post: by
2 posts views Thread by RgeeK | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.