![]() 56
Access VBA Programming for the Absolute Beginner, Second Edition
Do not count on VBA to always convert data successfully for you. In fact, it is good program-
ming practice to always use the Val function to convert strings to numbers when performing
numeric calculations on string variables or properties.
In addition to variables most programming languages, including VBA, provide support for con-
stants. Unlike variables, constants retain their data values throughout their scope or lifetime.
Constants are useful for declaring and holding data values that will not change during the
life of your application. Unless they are declared in a standard code module using the
Public
keyword, constants cannot be changed once declared.
In VBA, you must use the
Const
statement to declare a constant as revealed in the next state-
ment, which creates a constant to hold the value of
PI
.
Const PI = 3.14
For readability, I like to capitalize the entire constant name when declaring constants in my
VBA code. This way, they really stick out for you and other programmers when seeing their
name amongst other variable names in program code.
Variable Naming Conventions
Depending on the programmer and programming language, there are a number of popular
naming conventions for variables. I like to use a single prefix that denotes data type, followed
by a meaningful name with first letters capitalized for each word comprising the variable
name. Table 2.3 lists some common data types with a sample variable name and purpose.
Data Type
Purpose
Sample Variable Name
Boolean
Determines if a user is logged in
bLoggedIn
Currency
Specifies an employee’s salary
cSalary
Date
Employee’s hire date
dHireDate
Double
Result of calculation
dResult
Integer
Tracks a player’s score
iScore
Long
Current temperature
lTemperature
Single
Miles traveled on vacation
sMilesTraveled
String
Employee’s last name
sLastName
Const
A constant, which holds the current tax rate TAXRATE
T
ABLE
2.3 S
AMPLE
N
AMING
C
ONVENTIONS
|
|