Constants

team lib

Constants are static names for constant data. They are declared while you are writing your code, and can be used anywhere in the code, but they cannot be changed programmatically that's why they are called constants. They are a great way to improve the readability and maintainability of your code because, like variables , they have a name, and you can therefore use the name instead of the actual value in your code.

You add constants to your code using the Const statement. For example:

   Const clSpeedOfLight = 299792458   

Once defined you can use the name in your code instead of the value. So what's the use of this? Well firstly it makes your code more readable. Seeing 299792458 in your, or someone else's, code might not be very meaningful (unless you're a rocket scientist), but seeing clSpeedOfLight instantly gives you more information. Another great reason is that should you want to change the value of a constant, you only have to change it once, where it is defined, and not everywhere in the code.

You can use constants anywhere in code, even in other constant definitions:

   Const csMinute = 60     Const csHour = csMinute * 60     Const csDay = 24 * csHour   

Constants also allow you to declare a type, which can improve the error checking that VBA can do:

   Const clSpeedOfLight As Long = 299792458     Const csCleverPerson As String = "Albert Einstein"   

Notice that there are also some naming standards applied to these constants this is the cs and cl you see at the start of the constant name.

 
team lib


Beginning Access 2002 VBA
Beginning Access 2002 VBA (Programmer to Programmer)
ISBN: 0764544020
EAN: 2147483647
Year: 2003
Pages: 256

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net