String (variable length) 10 bytes + string length

String (fixed length) Variant (with numbers) Variant (with characters)

length of string 16 bytes

22 bytes + string length



-922,337,203,685,4775808 to 922,337,203,685,477.5807

1 January 100 to 31 December 9999

-I.797693I348623IE308 to -4.9406564584I247E-324 for negative values and from 4.9406564584I247E-324 to I.797693I3486232E308 for positive values

-32,768 to 32,767

-2,I47,483,648 to 2,I47483,647

-3.402823E38 to -I.40I298E-45 for negative values and from I.40I298E-45 to 3.402823E38 for positive values up to approximately 2 billion (2A3I) characters

I to approximately 64K (2AI6) characters up to range of Double data type same as variable-length string

By default, VBA initializes your declared variables for you. Specifically, all number-based variables are initialized to zero (0), strings are initialized to empty string (""), and Boolean variables are initialized to False. This may seem trivial, but it is a nice feature that is not offered in every programming language.

To assign a data type to a variable, simply supply a data type name in the variable declaration using the As clause.

Dim myName As String

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.

0 0

Post a comment