Numeric Variables Visual Basic for Applications 6
Objectives n In this tutorial, you will learn how to: n Reserve a numeric variable n Use an assignment statement to assign a value to a numeric variable n Perform calculations using arithmetic operators n Add a list box to an Excel worksheet n Use the Excel VLookup function in a procedure n Search a table in Word n Refer to the Access ADO object model in code n Use the Recordset Object’s Find method 6
Concept Lesson: Reserving a Procedure-level Numeric Variable n Dim statements can be used to reserve a procedure-level numeric variable, which is a memory cell that can store a number only n Variables assigned either the Integer or the Long data type can store integers, which are whole numbers n 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 6
Data Types Used to Reserve Numeric Variables 6
Reserving a Procedure-level Numeric Variable n The memory requirement of a data type is an important consideration when coding a procedure n Long data type uses 4 bytes of memory, while the Integer data type uses only 2 bytes 6
Using an Assignment Statement to Assign a Value to a Numeric Variable n When variablename is the name of a numeric variable, a value can be a number, more technically referred to as a numeric literal constant, or it can be a numeric expression 6
Assigning a Numeric Literal Constant to a Numeric Variable n A numeric literal constant is simply a number n A numeric literal constant cannot contain a letter, except for the letter E, which is used in exponential notation n Numeric literal constants also cannot contain special symbols, such as the % sign, the $ sign, or the comma n They also cannot be enclosed in quotation marks (“”) or number signs (#), because numbers enclosed in quotation marks are considered string literal constants, and numbers enclosed in number signs are considered date literal constants 6
Assigning a Numeric Literal Constant to a Numeric Variable 6
Assigning a Numeric Expression to a Numeric Variable 6 n Numeric expressions can contain items such as numeric literal constants, variable names, functions, and arithmetic operators n The precedence numbers represent the order in which the arithmetic operations are processed in an expression n 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 6 n 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
Examples of Assignment Statements Containing Numeric Expressions 6
Summary To reserve a procedure-level numeric variable: n Use the Dim statement. The syntax of the Dim statement 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 n 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: n Use an assignment statement in the following syntax: variablename=value 6
Excel Lesson: Viewing the Paradise Electronics Price List n To open Jake’s workbook and view the Paradise Electronics price list, use the steps on pages 338 and 339 of the textbook 6
Creating a List Box n A list box is one of several objects, called controls, that can be added to a worksheet n You typically use a list box to display a set of choices from which the user can select only one n List boxes help prevent errors from occurring in the worksheet n To add a list box control to the Computers worksheet, use the steps on pages 340 to 342 of the textbook 6
Control Toolbox Toolbar 6
Placement of Crosshair 6
List Box Control Drawn on the Worksheet 6
Creating a List Box 6 n To change the value assigned to several of the list box’s properties, use the steps on pages 343 to 347 of the textbook n The Object box, located immediately below the Properties window’s title bar, displays the name and type of the selected object
Creating a List Box n The Properties list, which can be displayed either alphabetically or by category, has two columns n The right column, called the Settings box, displays the current value, or setting, of each of those properties 6
List Box Shown in the Correct Size 6
Coding the List Box’s DblClick Event Procedure n A list box’s DblClick event procedure occurs when the user double-clicks an item in the list 6
Variables Used by the List Box’s DblClick Event Procedure 6
Coding the List Box’s DblClick Event Procedure n To begin coding the DblClick event procedure, use the steps on pages 348 and 349 of the textbook 6
Code Window Showing the Additional Two Lines of Code 6
Using the Excel VLookup Function in a Procedure n You can use Excel’s VLookup function to search for, or “look up,” a value located in the first column of a vertical list, and then return a value located in one or more columns to its right n In the VLookup function’s syntax, lookup_value is the value to be found in the first column of table, which is the location of the range that contains the table of information n When range_lookup is True, or when the argument is omitted, the VLookup function performs a case-insensitive approximate search, stopping when it reaches the largest value that is less than or equal to the lookup_value 6
Syntax of the VLookup Function 6
Examples of the VLookup Function 6
Using the Excel VLookup Function in a Procedure n To complete the DblClick event procedure, use the steps on pages 353 and 354 of the textbook 6
Using the Excel VLookup Function in a Procedure n To test the list box’s DblClick event procedure, use the steps on pages 354 and 355 of the textbook 6
Word Lesson: Coding the UpdateMembership Procedure n Begin by opening this document and viewing the code template for the UpdateMembership procedure, which already has been inserted into a module n To open Pat’s document and view the UpdateMembership procedure, use the steps on pages 362 to 364 of the textbook 6
Membership Document 6
Pseudocode for the UpdateMembership Procedure 6
Variables Used by the UpdateMembership Procedure 6
Coding the UpdateMembership Procedure 6 n To begin coding the UpdateMembership procedure, use the steps on pages 365 and 366 of the textbook
Code Window Showing the Additional Two Lines of Code 6
Searching a Table n 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 n To continue coding the UpdateMembership procedure, use the steps on pages 366 to 368 of the textbook 6
Moving the Insertion Point to the Beginning of the Document n You can use the Selection object’s HomeKey method to move the insertion point to a different area in the document n The HomeKey method, whose syntax is expression.HomeKey Unit:=unit, corresponds to the functionality of the Home key on your keyboard n 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 n To complete and test the UpdateMembership procedure, use the steps on pages 369 and 370 of the textbook 6
Completed UpdateMembership Procedure 6
Updated Membership Document 6
Access Lesson: The ADO Object Model n The ADO (ActiveX Data Objects) object model contains all of the objects needed to manage the records contained in one or more tables 6
The ADO Object Model n 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 n 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 6
Coding the PaymentUpdate Procedure n Begin by opening this database and viewing the Payments table n To open Professor Martinez’s database and view the Payments table, use the steps on pages 376 to 378 of the textbook 6
Pseudocode for the PaymentUpdate Procedure 6
Variables Used by the PaymentUpdate Procedure 6
Coding the PaymentUpdate Procedure 6 n To begin coding the PaymentUpdate procedure, use the steps on pages 379 and 380 of the textbook
Using the Recordset Object’s Open Method 6 n You use the Recordset object’s Open method to open a recordset n The syntax of the Open method is recordset.open Source:=datasource, ActiveConnection:=connection, CursorType:= cursortype, LockType:=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 CursorType and LockType Arguments 6
Using the Recordset Object’s Open Method n The cursortype and locktype arguments in the syntax can be one of the constants n The LockType 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 n To continue coding the PaymentUpdate procedure, use the steps on page 382 of the textbook 6
Partially Completed PaymentUpdate Procedure 6
Using the Recordset Object’s Find Method n You can use the Recordset Object’s Find method to search for a value contained in a field in the recordset n The syntax of the Find method is recordset.Find Criteria:=criteria n To complete the PaymentUpdate procedure, use the steps on pages 384 and 385 of the textbook 6
Examples of Using the Find Method to Search the rstPays Recordset 6
Completed PaymentUpdate Procedure 6
Using the Recordset Object’s Find Method 6 n To test the PaymentUpdate procedure, use the steps on page 386 of the textbook