Working with Numeric Variables Unit 6 Visual Basic

Working with Numeric Variables (Unit 6) Visual Basic for Applications

Objectives § In this unit, you will learn how to: § Reserve a numeric variable § Perform calculations using arithmetic operators § Add a list box to an Excel worksheet § Use the Excel VLookup function in a procedure § Search a table in Word § Refer to the Access ADO object model in code § Use the Recordset Object’s Find method

Concept Lesson: Discussing numeric variables § Use Dim statements to reserve a procedure-level numeric variable, which is a memory cell that can store a number only § Variables assigned either the Integer or the Long data type can store integers, which are whole numbers § The differences between the two data types are in the range of numbers each type can store and the amount of memory each type needs to store the numbers § Open VBE and search for Data Type Summary

Data Types Used to Reserve Numeric Variables Search for “Data Type Summary” in VBE help box Exhibit 6 -1: The datatypes used to reserve numeric variables

Reserving a Procedure-level Numeric Variable § The memory requirement of a data type is an important consideration when coding a procedure § Long data type uses 4 bytes of memory, while the Integer data type uses only 2 bytes Exhibit 6 -2: Some examples of Dim statements that reserve numeric variables

Assigning a Numeric Literal Constant to a Numeric Variable § A numeric literal constant is simply a number § A numeric literal constant cannot contain a letter, except for the letter E, which is used in exponential notation § Numeric literal constants cannot contain special symbols, such as the % sign, the $ sign, or the comma § They also cannot be enclosed in § quotation marks (“”) § or number signs (#),

Assigning a Numeric Literal Constant to a Numeric Variable Exhibit 6 -3: Some examples of valid and invalid numeric literal constants Exhibit 6 -4: Some examples of assignment statements that assign numeric literal constants to variables

Assigning a Numeric Expression to a Numeric Variable § Numeric expressions can contain items such as numeric literal constants, variable names, functions, and arithmetic operators § The precedence numbers represent the order in which the arithmetic operations are processed in an expression § You can use parentheses to override the order of precedence because operations within parentheses always are performed before operations outside of parentheses

Assigning a Numeric Expression to a Numeric Variable § When you create a numeric expression that contains more than one arithmetic operator, keep in mind that VBA follows the same order of precedence as you do when evaluating the expression Exhibit 6 -5: The arithmetic operators and their order of precedence

Examples of Assignment Statements Containing Numeric Expressions Exhibit 6 -6: Some examples of assignment statements containing numeric expression

Summary To reserve a procedure-level numeric variable: § Use the Dim statement. The syntax is § Dim variablename As datatype where variablename represents the name of the variable (memory cell) and datatype is the type of data the variable can store § Variable names must begin with a letter and they can contain only letters, numbers, and the underscore To assign a value to a numeric variable: § Use an assignment statement in the following syntax: variablename=value

Excel Lesson: Using numeric variables in Excel Open Jake’s workbook and view the Paradise Electronics price list.

Creating a List Box § A list box is one of several objects, called controls, that can be added to a worksheet § You typically use a list box to display a set of choices from which the user can select only one § List boxes help prevent errors from occurring in the worksheet § To add a list box control to the worksheet, use the Control Toolbox toolbar

Inserting Controls § You should enable Developer tab in Office 2007 to insert Controls § There are two types of controls: § Form Controls § Active. X Controls § Form controls were the original controls and are much more simple § Active. X controls offer more formatting properties § MS does not include Active. X controls for the Mac

Control Toolbox Toolbar (Search for Overview of Form Controls) Exhibit 6 -7: The Control Toolbox toolbar

Creating a List Box § To change the value assigned to several of the list box’s properties, use the Properties window § The Object box, located immediately below the Properties window’s title bar, displays the name and type of the selected object Exhibit 6 -8: The Properties window

Creating a List Box § The Properties list, which can be displayed either alphabetically or by category, has two columns § The right column, called the Settings box, displays the current value, or setting, of each of those properties

List Box Control Drawn on the Worksheet Exhibit 6 -9: The list box drawn on the worksheet

Coding the List Box’s Dbl. Click Event Procedure A list box’s Dbl. Click event procedure occurs when the user double-clicks an item in the list Exhibit 6 -10: The pseudocode for the list box’s Dbl. Click event procedure

Variables Used by the List Box’s Dbl. Click Event Procedure Exhibit 6 -11: The variables used by the list box’s Dbl. Click event procedure

Using the Excel VLookup Function in a Procedure § You can use the VLOOKUP function to search the first column of a range of cells, and then return a value from any cell on the same row of the range. § When range_lookup is True (default) the VLookup function § performs a case-insensitive approximate search § stops when it reaches the largest value that is less than or equal to the lookup_value

Syntax of the VLookup Function Exhibit 6 -12: The syntax of the Vlookup function

Examples of the VLookup Function Exhibit 6 -13: Some examples of the Vlookup function

Word Lesson: Using numeric variables in Word § Open Pat’s document and view the Update. Membership procedure.

Pseudocode for the Update. Membership Macro Exhibit 6 -14: The pseudocode for the Updatemembership procedure

Variables Used by the Update. Membership Procedure Exhibit 6 -15: The variables used by the Update. Membership procedure

Searching a Table In Word, you can search a column in a table first by selecting the column, and then using the Execute method of the Find object to locate the desired value

Moving the Insertion Point to the Beginning of the Document § You can use the Selection object’s Home. Key method to move the insertion point to a different area in the document § The Home. Key method, whose syntax is expression. Home. Key Unit: =unit, corresponds to the functionality of the Home key on your keyboard § In Microsoft Word, a story is defined as an area of a document that contains a range of text that is distinct from other areas of text in the document

Using numeric variables in Access The ADO Object Model § The ADO (Active. X Data Objects) object model contains all of the objects needed to manage the records contained in one or more tables Exhibit 6 -16: A portion of the ADO object model showing the most commonly used ADO objects

The ADO Object Model § A Connection object represents the physical connection between an Access database and a data provider, which is a set of complex interfaces that allows ADO objects to use the data stored in a database § Immediately below the Connection object in the ADO object model is the Recordset object, which represents either all or a portion of the records (rows) contained in one or more tables

Coding the Payment. Update Procedure § Begin by opening this database and viewing the Payments table § Open Professor Martinez’s database and view the Payments table.

Pseudocode for the Payment. Update Procedure Exhibit 6 -17: The pseudocode for the Payment. Update procedure

Variables Used by the Payment. Update Procedure Exhibit 6 -18: The variables used by the Payment. Update procedure

Using the Recordset Object’s Open Method § You use the Recordset object’s Open method to open a recordset § The syntax of the Open method is recordset. open Source: =datasource, Active. Connection: =connection, Cursor. Type: = cursortype, Lock. Type: =locktype, where recordset is the name of a Recordset object variable, datasource specifies the data source, and connection is the name of a Connection object variable

Valid Constants for the Open Method’s Cursor. Type and Lock. Type Arguments Exhibit 6 -19: The valid constants for Cursor. Type and Lock. Type arguments

Using the Recordset Object’s Open Method § The cursortype and locktype arguments in the syntax can be one of the constants § The Lock. Type argument prevents more than one user from editing a specific record at the same time by locking the record, making it unavailable to other users

Using the Recordset Object’s Find Method You can use the Recordset Object’s Find method to search for a value contained in a field in the recordset The syntax of the Find method is recordset. Find Criteria: =criteria

Examples of Using the Find Method to Search the rst. Pays Recordset Exhibit 6 -20: Some examples of using the Find method to search the rst. Pays recordset
- Slides: 38