Data Types and Operations

Declarations, Variables, and Constants
Variable - a uniquely named storage location that contains data that changes during program execution Constant - a uniquely named storage locations that contains data that does not change during program execution

Declarations, Variables, and Constants
Rules for Naming Variables
– Must begin with an alphabetic character – Can’t contain a period or type-declaration characters such as %, &, !, #, @ or $ – Must be unique with same scope – Must be no longer than 255 characters – Should not reserved word (See Appendix A)

Declaring Variables
Declaration statement - nonexecutable code that sets aside storage locations for future use Local variables - declared within a procedure or function Global variables - declared in the general section of the application

Declaring Variables
• Declare variables using the Dim or Static statements
Dim statement - value of variable preserved only until procedure ends Static statement - value of variable preserved the entire time the application is running

The Dim Statement
Dim variablename As datatype
Where

Dim is required variablename should be a descriptive name As is required datatype is one of the following types: Boolean, Byte, Date, Integer, Long, Single, Double, Currency, String, Object or Variant

Declaring Variables
Data Types
– – – – – – – – – – Boolean - True or false Date - From Jan 1, 100 to Dec 31, 9999 Integer - Numbers without a decimal point Long - Long integer Single - Numbers with a decimal point Double - Long Single Currency - Dollar amounts String - Character and alphanumeric data Object - Any object reference such as Word document Variant - default, can hold any data type

Assigning Values to Variables
Variablename = value
Where variablename is the descriptive name of the variable = is the assignment operator value is the value the variable will contain Examples: Number1 = 5 FirstName = “Steve” LoanAmount = 67.38 Length = 17.8 Note: Order is important. Variable name always on the left, and value on the right.

Declaring Constants
Const constantname As datatype = value
Where Const is required constantname is the descriptive name of the constant As is required datatype is the type of data the constant will contain = is the assignment operator value is the value of the constant Examples: Const Pi As Single 3.14159265358979 Const MaxNumber As Integer = 100

The Sub Statement
Private Sub controlname_eventname( ) Statements End Sub
Where Private is the default procedure type Sub indicates beginning of procedure controlname is name of associated control _ (underscore) required separator eventname is name of corresponding event ( ) set of parentheses is required End Sub indicates end of a procedure

The Sub Statement
Example
Private Sub cmdCalcTriangle_Click Dim Base As Single Dim Height As Single Dim Area As Single Area = 1 / 2 * (Base * Height) End Sub

Sign up to vote on this title
UsefulNot useful