Declaring Variables

  • Thread starter Thread starter Berny
  • Start date Start date
B

Berny

Can anyone help me on this one or at lease tell me if it can be done.

I'm trying to declare a variable once that can be used by all of the
functions/procedures in the module/project.

dim strVar as string

strVar = "1.1b"

I can't seem to make it work.
 
Your Dim strVar As String needs to be in a module (not in the code behind a
form), and it must be at the beginning of the module (i.e.: not inside any
function or sub).

As well, to make it available everywhere, you need to use Global strVar As
String (or Public strVar As String).

The assignment statement, strVar = "1.1b", can go anywhere (but obviously
the assignment must be made before the variable is used anywhere!)
 
Berny said:
Can anyone help me on this one or at lease tell me if it can be done.

I'm trying to declare a variable once that can be used by all of the
functions/procedures in the module/project.

dim strVar as string

strVar = "1.1b"

I can't seem to make it work.

You have to declare the variable at the module level in a standard
module (not a form, report, or class module), and it must be declared
with the Public keyword; e.g.,

Public strVar As String

You should be aware that under some circumstances the value of a global
variable can be lost. For example, an unhandled error will cause your
VB project to be reset, and that will erase all the values of public
variables. So if you want to use a global variable to hold data for an
extended period, you'd better include code to check that the variable
still has a value, and reload it if necessary.
 
Maybe I misunderstand you, but, in addition of what Douglas and Dirk are
saying and in the case you want to assign a value that is not going to
change in the whole application, you better use this other way to do it

Public Const strVAr As String = "1.1b"

if this is not the case, just forget it

:-)

--
Saludos desde Barcelona
Juan M. Afan de Ribera
<MVP Ms Access>
http://www.juanmafan.tk
http://www.clikear.com/webs4/juanmafan
 
Back
Top