Using Variables and Constants Definitions: A variable or constant is a named memory loction used for storing information. A literal is a value that is an exact representation of the information. In order to use information, we need to store it in the memory of the computer. We have to name that location so that we can find the information again. The name acts like an address on a mailbox. A variable is a piece of information that can change during the course of a program. For example, if you have a program to process employee payroll, you might have a variable for storing the name of the employee that you are currently processing the payroll for. The literal name of the employee stored in the variable changes as you process one employee after the other. A constant is a piece of information that never changes during the life of the program. For example, you may have a program that calculates the area of a circle when the use enters a radius. The formula uses the value of pi, which is about 3.14159. You can either write out the literal value "3.14159" everytime you need it, or you can create a constant named "PI" that stores the value for you. Declaring Variables There are three parts to declaring a variable: It's SCOPE - What parts of the program can use the variable? It's NAME - How you are going to refer to the variable? It's TYPE - What kind of information is it going to store? How is scope determined? Scope is determined based on where the variable is declared and what keyword is used to declare it. A variable declared inside a procedure is local to the procedure, and is declared using the keyword Dim. Only that procedure can use the variable, no other procedure can use it. A variable declared in the General Declarations section of a form (just below the "Option Explicit" statement) can use any one of these keywords: Dim, Private, Public. Dim or Private means the variable can be used by any procdere in the form, but not by anything else in the program. Public allows the variable to be used by anything in the project. A variable declared in the General Declarations section of a code module (just below the "Option Explicit" statement) can use any one of these keywords: Private, Public, Global. Private means the variable can be used by any procdere in the module, but not by anything else in the program. Public and Global allows the variable to be used by anything in the project. To declare a variable to store the name of an item: Dim strItemName as String To declare a variable to store the quanity of the item in stock: Dim intItemQty as Integer Dim lngItemQty as Long To declare a variable to store the price of the item: Dim curItemPrice as Currency Declaring Constants Constant declarations are different from variable declarations in two ways: 1. Use the keyword Const 2. Assign a value To declare a constant to store the value of pi: Const PI as Double = 3.14159 Using Constants and Variables Variables are assigned values the same way we change property values for controls, by using an assignment statement. Let's look at an example that puts everything together in one place. Let's say we have a program that calculates the area of a circle. The form has a TextBox named txtRadius, a button named cmdCalcArea, and a label named lblArea. Private Sub cmdCalcArea_Click() Const PI as Double = 3.14159 Dim dblRadius as Double Dim dblArea as Double dblRadius = Val(txtRadius.Text) dblArea = PI * dblRadius * dblRadius lblArea.Caption = dblArea End Sub