global constants

T

tina

hi folks. this is probably a stupid question, but i'm going to <cringe> ask
it anyway:
is it possible to create a global constant programmatically? or to change
the value of an existing global constant programmatically?
if i should be looking to another resource for info, just point me in the
right direction and give me a shove!
tia
tina :)
 
V

Van T. Dinh

Yes. You can declare the global constants or global variables in a Standard
Module. The constants / variables will then be available anywhere in your
code.
 
P

Paul

Yes..it is possible to create a global constant programmatically, although
I'm not sure of your purpose in doing so. Are you creating a utility that
automatically writes code?

Is is not possible to change the value of an existing global constant...it
goes completely against the definition of a constant. Constants do not
change value. However, you can declare a global VARIABLE and its value can
be changed.

A constant will be declared in code by use of the keyword CONST followed by
a name, equal sign, and a value. Variables are declared by using the
keywords (depending on scope and usage) Dim, Private, Public, Global, or
Static followed by a name, the word As, and a data type. See VBA help
concerning the keywords mentioned for additional info.

--
Michael Badnarik for President '04
Libertarian...the freedom party
www.lp.org
www.badnarik.org

"If you are in prison and your chances are 50% for execution by electric
chair, 45% for execution by lethal injection, and 5% for escape, are you
just going to vote for the chair because it is the likeliest outcome?" Vote
Libertarian and live.
 
M

Marshall Barton

tina said:
hi folks. this is probably a stupid question, but i'm going to <cringe> ask
it anyway:
is it possible to create a global constant programmatically? or to change
the value of an existing global constant programmatically?
if i should be looking to another resource for info, just point me in the
right direction and give me a shove!


No, to both (unless the code is open in design view and you
really don't want to go there).

A constant is something that can not be changed. If it
could be changed, it wouldn't be constant, that's what
variables are for.

If you'll explain what you're trying to accomplish, maybe
someone will be able to find a way to do whatever you're
after without using a constant.
 
P

Paul

Actually, you could create a routine that would write code...including
constants. I've written routines of this nature that are in a library I use
as an aid during development...to automatically create a form validation
routine for example. You are correct in so far that this could not be done
in a compiled app, and probably wouldn't be desired in a production app.
Nevertheless, it isn't entirely clear what the OP is trying to accomplish.

--
Michael Badnarik for President '04
Libertarian...the freedom party
www.lp.org
www.badnarik.org

"If you are in prison and your chances are 50% for execution by electric
chair, 45% for execution by lethal injection, and 5% for escape, are you
just going to vote for the chair because it is the likeliest outcome?" Vote
Libertarian and live.
 
T

tina

so there is a way to do it programmatically, if you open a standard module
to design view? can you tell me how i would write the code to do that? or
where i might get instructions or an example? or maybe i misunderstood your
answer?
 
M

Marshall Barton

tina said:
so there is a way to do it programmatically, if you open a standard module
to design view? can you tell me how i would write the code to do that? or
where i might get instructions or an example? or maybe i misunderstood your
answer?

Read up on the Module object in Help. It has several
methods that are useful for this kind of thing:
Find
AddFromString
InsertLines
InsertFile
. . .
You'll also need a reference to the appropriate version of
the VBA Extensibility library.

I will caution you again that this is very definitely NOT
something you should do in a running application. These
methods are only provided for the purpose of creating a
Wizard.

Don't forget that you only need to declare a Constant once
in your application by placing it in a standard module:
Public Const TWIPS As Long = 1440
So, there is no need to automate inserting a single line,
one time in an application. Besides, even if you're adding
constant statements to many applications using Automation,
you still have to make further changes to use the new
constant. Also, note that you can not refer to VBA
constants in queries or control source expressions.
--
Marsh
MVP [MS Access]


 
T

tina

thanks, Marshall! and Van and Paul, too! your time and expertise
appreciated, as always. :)


Marshall Barton said:
tina said:
so there is a way to do it programmatically, if you open a standard module
to design view? can you tell me how i would write the code to do that? or
where i might get instructions or an example? or maybe i misunderstood your
answer?

Read up on the Module object in Help. It has several
methods that are useful for this kind of thing:
Find
AddFromString
InsertLines
InsertFile
. . .
You'll also need a reference to the appropriate version of
the VBA Extensibility library.

I will caution you again that this is very definitely NOT
something you should do in a running application. These
methods are only provided for the purpose of creating a
Wizard.

Don't forget that you only need to declare a Constant once
in your application by placing it in a standard module:
Public Const TWIPS As Long = 1440
So, there is no need to automate inserting a single line,
one time in an application. Besides, even if you're adding
constant statements to many applications using Automation,
you still have to make further changes to use the new
constant. Also, note that you can not refer to VBA
constants in queries or control source expressions.
--
Marsh
MVP [MS Access]


 

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