General variables?

N

Nicolae Fieraru

Hi All,

I work on a database and I use some paths (strings like
"C:\MyPath\MyFolder") in VBA for different forms and reports. If I move the
database to the customer's machine, I will have to edit the whole code in
every location to make it work again. Is there a way of defining some
general variables which I can use in any of the modules?

Regards,
Nicolae
 
P

Patrick Wolf

Hi Nicolae,

Sure you just make a module named eg. "modVars" and put all public variables
in it.
Then in the Form_Load or Form_Open event of ALL of your forms you call the
routine InitVars.
It is seen as good coding to prefix global variables with a g..... (for
global).

All the best
Patrick
www.patrickwolf.net

---------------------------------------------------------------------
Module modVars
---------------------------------------------------------------------
Public gImagePath as string
Public const gAppVersion = "V.0.25"
Private bolInitAlready AS boolean

Public Sub InitVars ()
' Only Init the Vars on Startup
if bolInitAlready then exit sub
gImagePath = "c:\Path"
bolInitAlready = TRUE
End Sub
 
L

Larry Daugherty

The practice of using global variables is generally discouraged.
There is a real danger that the value contained in a global variable
may have been changed somewhere in your program that you've
overlooked. You are well aware of what's going on when you create and
first use them but you may have forgotten some things over the years
as you add and modify code.

A common temptation to se global variables is the tracking of user
options and configuration options. In most of my applications I place
a table, tblUserOptions to hold them. I keep it simple with an
autonumber primary key, text to name the variable - Back End Pathname,
text to hold the argument to that variable - C:\MyPath\MyFolder, and a
text field for notes about the variable. I provide a form to manage
the information. The variable name is entered by the programmer and
is not changeable by the user.

HTH
 
A

adsl

Nicolae Fieraru said:
Hi All,

I work on a database and I use some paths (strings like
"C:\MyPath\MyFolder") in VBA for different forms and reports. If I move
the database to the customer's machine, I will have to edit the whole code
in every location to make it work again. Is there a way of defining some
general variables which I can use in any of the modules?

Regards,
Nicolae
 
A

adsl

Patrick Wolf said:
Hi Nicolae,

Sure you just make a module named eg. "modVars" and put all public
variables in it.
Then in the Form_Load or Form_Open event of ALL of your forms you call the
routine InitVars.
It is seen as good coding to prefix global variables with a g..... (for
global).

All the best
Patrick
www.patrickwolf.net

---------------------------------------------------------------------
Module modVars
---------------------------------------------------------------------
Public gImagePath as string
Public const gAppVersion = "V.0.25"
Private bolInitAlready AS boolean

Public Sub InitVars ()
' Only Init the Vars on Startup
if bolInitAlready then exit sub
gImagePath = "c:\Path"
bolInitAlready = TRUE
End Sub
 
A

adsl

Larry Daugherty said:
The practice of using global variables is generally discouraged.
There is a real danger that the value contained in a global variable
may have been changed somewhere in your program that you've
overlooked. You are well aware of what's going on when you create and
first use them but you may have forgotten some things over the years
as you add and modify code.

A common temptation to se global variables is the tracking of user
options and configuration options. In most of my applications I place
a table, tblUserOptions to hold them. I keep it simple with an
autonumber primary key, text to name the variable - Back End Pathname,
text to hold the argument to that variable - C:\MyPath\MyFolder, and a
text field for notes about the variable. I provide a form to manage
the information. The variable name is entered by the programmer and
is not changeable by the user.

HTH
 

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