Volume 3: Programming with the Visio Object Model disclaimer

Download 0.78 Mb.
Size0.78 Mb.
1   2   3   4   5   6   7   8   9   ...   36

Data Types

In most programming languages, data types are specified for each variable, such as String, Integer, Date, Boolean, etc. In VBA, you specify the data type when you declare a variable with the As keyword:

Dim BabyFirstWord As String

BabyFirstWord = “Googoo”

To create concise and fast code, it is recommended to explicitly specify a data type. If you use Option Explicit it will force all of your variables to have explicit data types. You can of course declare a variable without specifying a data type. In this case, VB assigns the variable to a special data type called Variant.


When you do not specify a data type for a variable, the variable is assigned to the data type called Variant. The Variant is a special type of variable that can store a value of any type. Depending on the value and the context VBA will automatically assign the Variant one of the following sub-types: Null, Numeric, String, Date/Time, Boolean, or Object.
NULL Data Type

A NULL subtype is an unusual subtype that is used in conjunction with databases. NULL refers to a field that contains no data. NULL does not mean zero, or even empty. Zero is a valid value, and empty is a data type that has nothing assigned to it yet. NULL means “nothing”, no data and no data type.
Numeric Data Types

Numeric Data Types include the following:

  • Integer – Whole numbers within the range of -32,768 to 32,767.

  • Byte – Integers within the range of 0-255

  • Long – Similar to Integer, but supports ranges from -2,147,483,648 to 2,147,483,647

  • Single – A single precision floating point number

  • Double – A double precision floating point number

  • Currency – Accepts numbers up to four decimal places
String Data Type

A string holds textual information or words. String values are usually put in quotes. You cannot perform mathematical functions on strings, even if numbers are present in the string:

Number1 = “12”

Number2 = “14”

Number3 = Number1 + Number2 ‘Will Produce “1214”

Date/Time Data Types

Date/Time data types store dates and times, and must be enclosed in # characters:

myBirthDate = #5/4/1965#

Boolean Data Types

Boolean data types store values as either TRUE, or FALSE.
Object Data Types

The Object data type stores a reference to an object. Objects will be discussed further a bit later in this supplement.
Naming Variables

It is important to establish a good naming convention for your variables. Your naming convention should be easily understood by another developer who might look at your code later. Some tips for naming:

NameFirst and NameLast are better than FirstName and LastName because they will appear together in a search.

Within a procedure or function you may want to prefix your variables with a p_ or f_ type code letter.

Create names with multiple words, capitalizing the first letter of each word.

1.Using Hungarian Notation

The Hungarian notational convention is to use the first three letters of the variant’s name to distinguish the sub-type, as shown in the following table:

Data Type









Date / Time





















For your applications, you do not have to use the Hungarian notation, but something similar is recommended.


Naming conventions and coding style standards evolve over time. Check if your organization has a defined naming convention and coding standard to use.

Manipulating Variables

In order for variables to be of any use, you will need to manipulate them in some way. You can manipulate variables using a number of different operators and by concatenating or converting them.

Assignment Operator

The assignment operator is the ‘equal’ (=) sign. With it, you can assign a value to variable. The variable name goes on the left, and the value to be assigned on the right side of the equals sign.

intMyFavoriteNumber = 11

The assignment operator can also be used to assign a new value to a variable, as in the following mathematically unsound formula:

intMyFavoriteNumber = intMyFavoriteNumber + 1

In the above case, it may look like we are saying 11 = 11 + 1. However, we are simply reassigning a new value to intMyFavoriteNumber.

Comparison Operators

Comparison Operators are the following:

  • Equality: =

  • Inequality: <>

  • Less than: <

  • Greater than: >

  • Less than or equal to: <=

  • Greater than or equal to: >=
Arithmetic Operators

You can use the following arithmetic operators to perform mathematical operations on your variables:

  • Addition: +

  • Subtraction: -

  • Multiplication: *

  • Division: /

  • Exponentiation: ^

  • Negation: -

  • Modulus: MOD or \

Download 0.78 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   ...   36

The database is protected by copyright ©ininet.org 2022
send message

    Main page