Question Global Variable

mcdhappy80

Registered User.
Local time
Today, 17:29
Joined
Jun 22, 2009
Messages
347
I have rather silly question and I want just to check this.

If I put a global variable in a form class module, assign it a value right after declaring it, and then reference it in several functions in that module, those references will have that one assigned value at the top?

Thank You
 
No, what you will get is an error.
Why not simply try what you have asked?
 
No, what you will get is an error.
Why not simply try what you have asked?
I've tried what You suggested, but I still don't have the answer can I, and how achieve this what I have in mind. Is it possible to do this and what is the way?
Thank You
 
What is your question really? Will you instantiate the variable from a routine/function in your form? From which module will you be calling the value of the variable from, the form module or a class module?
 
>>If I put a global variable in a form class module, assign it a value right after declaring it,<<

Code:
Option Compare Database
Option Explicit


Public lngSomeLong As Long
lngSomeLong = 1234   [color=green]' < this will raise an error.[/color]


Private Sub Fred()
    Public lngSomeOtherLong As Long [color=green]' < this will raise an error.[/color]
    
    
End Sub

Both error messages should tell you why.
 
Here's what I've tried to explain, the scenario:

Code:
...
Option Explicit
Dim txtVar as string
txtVar = "Z00"

private_sub btn1_click()
...
   & "WHERE tbl1.txt1 = '" & txtVar "';"
...
private_sub btn2_click()
...
   & "WHERE tbl1.txt1 = '" & txtVar "';"
...
When I put it this way I think my real question should be, ...
... "could I use the same value of the variable declared outside sub procedures in multiple sub procedures inside the same class module?
 
Yes you can but you can’t assign a value to txtVar outside a procedure.
 
Just to clarify:

Code:
...
Option Explicit
Dim txtVar as string
[COLOR=Red]txtVar = "Z00"[/COLOR]
[/quote]ChrisO had already pointed out in the post prior to yours that the line highlighted in red will throw an error. You can't assign a value to that global variable there. That section of the module is called the Declarations section and it's meant for only declaring variables. The only type of variable you can assign a value to is a Constant because their values never change (just like the name implies). E.g.
[code]...
Option Explicit
Const myVar = "Zoo"
You won't be able to assign a value to this type of variable anywhere else in your whole project.

Code:
...
 [COLOR=Red]private_sub[/COLOR] btn1_click()
The signature of a sub routine isn't written that way. Replace the underscore with a space and you have yourself a sub routine.:)

Code:
...
  When I put it this way I think my real question should be, ... 
  ... "could I use the same value of the variable declared outside sub procedures in multiple sub procedures inside the same class module?[/quote]A global variable declared within an object's module (i.e. a form's module for example) can be assigned a variable from within any of the functions or sub routines within that module. If you wanted to change the value outside that (form's) module, then you have to use a Public Function declared within that (form's) module and your form MUST be open (not loaded, but open).

I hope this helps.
 
just one other thing

a DIM, or a CONST can NEVER be a GLOBAL variable/constant

these are local to the procedure or module in which they are declared
so a dim, or const at the the top of a form's code module makes that value available only to that form

furthermore when you close the form, the values are destroyed, and new variables asre created next time you open the form (unless you declare them static, which is different again)

a GLOBAL variable HAS to be in a CODE MODULE

so
PUBLIC CONST mysetting = 1
PUBLIC myvar as string

in a MODULE persist throughout the life of your programme, and are available to all forms/reports/queries. Note that even these can be destryoed if the program crashes (ie you get an unhandled error giving you the debug/end msgbox) depending how you respond to it.
 
Thank You guys for last couple of answers, they clarify a lot
 

Users who are viewing this thread

Back
Top Bottom