What is a standard way of declaring constants?

leok31

Registered User.
Local time
Yesterday, 19:22
Joined
Feb 25, 2014
Messages
36
I know that a public constant is available to all subs and modules (hope thats right). I also know that you can only declare these in a module, you can't for example declare these in the form.
So how what is the best way of declaring them?
At the moment I just have a module that has nothing else except for the 3-4 constants that I use throughout all my forms. Is that ok?

thanks
 
Yes, that sounds fine...

Are the constants related in any way?
 
thanks.
no they are not. I have a few strings that I want to use throughout the project and I want to set up the connection string there as well. I connect to SQL server many times and at the moment i keep on repeating the code on each connection.
It gets tedious when I need to make a small change to the connections.
 
I connect to SQL server many times and at the moment i keep on repeating the code on each connection.

Put the connection creation code into function in a Standard Module. Return a connection object.

Note however that the same connection can be shared. The connection can be stored as a Global variable. When using multiple connections I store them in a Collection (or more often a Dictionary because the index can be searched).

Test the State of the connection each time it is used in case it is closed and simply open it again if necessary.
 
thanks for that.
I don't think I understand how the global variable works.
So I understand that you can declare it once for the whole project, for example
Public strABC as string.
How I can use this variable everywhere without declaring it again.
But if I set it as something, for example
strABC = "Hello World", that doesn't seem to work.
So why would I use a Global Variable instead of a Constant?

I hope my question makes sense.
 
The key word in variables is "scope". It defines where a variable can be seen from.

A Public variable declared in the declarations section at the top of a Standard Module has a global scope. It exists for the whole time the application is open and is available across the whole project. Simply refer to its name.

A Public variable in the declarations section of a Class module (including Access Object Modules) is available to the project via the object.

eg From the project to a variable in a Form module: Forms!formname.variablename

A Private variable in the declarations section is available throughout the module. It exists while the module's object is open.

Variables dimmed inside a function or sub are only available to that function or sub.

Use variables instead of constants for objects such as a Connection. They can't be declared as constants.
 

Users who are viewing this thread

Back
Top Bottom