global dim & set

M

mcalex

Hi all
I'm a bit new to vb, and want to know if there is a way of declaring
and setting a global variable outside of any procedures. I have a
public boolean (debugOn) that runs my debug.print statements if true
and doesn't clutter the immediate window if false. Everything's fine
as long as I set it within procedures, but I cannot convince VBA to
set it outside of a procedure, ie at declaration time. Is this
possible? and if so, how?

Many thanks
Alex
 
A

Amedee Van Gasse

mcalex said:
Hi all
I'm a bit new to vb, and want to know if there is a way of declaring
and setting a global variable outside of any procedures. I have a
public boolean (debugOn) that runs my debug.print statements if true
and doesn't clutter the immediate window if false. Everything's fine
as long as I set it within procedures, but I cannot convince VBA to
set it outside of a procedure, ie at declaration time. Is this
possible? and if so, how?

Many thanks
Alex

Scope of variables:
Application: declare with Public
Module: declare with Private
Procedure: declare with Dim

Read this also:
Using Variables (Properly) In VBA
http://www.cpearson.com/excel/variables.htm
 
T

Tushar Mehta

To declare a variable 'outside' of a procedure put it at the module
level before any sub/function statement.

However, you cannot assign a value at declaration time. That would have
to happen through some procedure you execute. Of course, if the
variable is declard as a basic data type, XL/VBA will initialize it to
the default value (zero for numbers, zero-length string, or false for a
boolean).

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2004
 
A

Andrew Cushen

Also-

Declaring a variable at the module level, i.e. right
after "Option Explicit" at the top of the code window,
will make that variable accessible everywhere IN THAT
MODULE ONLY. To make a variable accessible anywhere in the
project, you need to declare it in a module, as Public,
like so:

Public strStringVar as String

First, you'll need to create the module: PROJECT | ADD
MODULE, click the New tab, click Open. And remember to
save the module!

You'd want to set the value of the variable somewhere at
the start of the program, such as the Form_Load() event of
the first form that loads in your project.

HTH,

-Andrew
=====================================================
-----Original Message-----
To declare a variable 'outside' of a procedure put it at the module
level before any sub/function statement.

However, you cannot assign a value at declaration time. That would have
to happen through some procedure you execute. Of course, if the
variable is declard as a basic data type, XL/VBA will initialize it to
the default value (zero for numbers, zero-length string, or false for a
boolean).

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2004
 
M

Mike Labosh

Open up any standard module (not a class or form module).
At the top of the code, you will see something like this:

Option Compare Database
Option Explicit

Right underneath those, do something like this:

Public g_bDebugMode As Boolean

When your application starts up, some code could set it to True.

Alternatively, you could declare the variable as a constant and then
manually change it when you want to see your messages:

Public Const DEBUG_MODE = True

--
Peace & happy computing,

Mike Labosh, MCSD

"Do not meddle in the affairs of dragons, for
you are crunchy, and taste good with ketchup"
 
M

mcalex

aha, this is what I was looking for. Constants can be assigned
outside of procedures, but variables can't.

Thx Mike
A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top