54
Access VBA Programming for the Absolute Beginner, Second Edition
With this declaration statement, I’ve created one variable of
String
data type called
myName
.
I can now use the
myName
variable in VBA statements to get and set data inside reserved memory,
to which the variable
myName
points. This concept is demonstrated in the following statement.
myName = “Emily Elizabeth"
Notice when assigning data to string variables that the data on the right side must be
enclosed with double quotes. Moreover, VBA programmers can use the concatenation operator
(
&
) to glue two or more strings together. The next few VBA statements reveal VBA string
concatenation.
Dim myTitle As String
myTitle = “Access VBA “ & “Programming for the “ & “Absolute Beginner"
Me.Caption = myTitle
In the preceding example, I successfully assigned the contents of the
myTitle
variable to the
Caption
property of the form, which works because both the
String
variable and
Caption
property store string data types.
Numbers however, do not require double quotes when used in assignment statements.
Dim mySalary As Double
mySalary = 50000.55
myBalance = –457.23
Understanding the difference between string data and string variables is an
important concept in beginning programming. Beginning programmers often
forget to surround text with double quotes when assigning data to string based
variables or properties. Forgetting to do so can cause compile-time errors.
Study the next program statement and see if anything strikes you as weird.
Dim mySalary As Double
Me.Caption = mySalary
It’s intriguing that I can assign the variable
mySalary
(a
Double
) to a property such as
Caption
,
which holds
String
data types. After executing, the value in the
Caption
property is now
“50000.55"
and not
50000.55
.
Many languages such as C language, would not like the proceeding assignment statement
one bit. This is because many languages require you to convert or cast data of one data type
prior to assigning the value to a container of different data type.
Previous Main Next