Numeric Variables Visual Basic for Applications 6.

53
Numeric Variables Visual Basic for Applications 6

Transcript of Numeric Variables Visual Basic for Applications 6.

Page 1: Numeric Variables Visual Basic for Applications 6.

Numeric Variables

Visual Basic for Applications

6

Page 2: Numeric Variables Visual Basic for Applications 6.

Objectives

In this tutorial, you will learn how to: Reserve a numeric variable Use an assignment statement to assign a value to 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

6

Page 3: Numeric Variables Visual Basic for Applications 6.

Concept Lesson:Reserving a Procedure-level

Numeric Variable Dim statements can be used 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

6

Page 4: Numeric Variables Visual Basic for Applications 6.

Data Types Used to Reserve Numeric Variables

6

Page 5: Numeric Variables Visual Basic for Applications 6.

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

6

Page 6: Numeric Variables Visual Basic for Applications 6.

Using an Assignment Statement to Assign a Value

to a Numeric Variable

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

Page 7: Numeric Variables Visual Basic for Applications 6.

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 also cannot contain special symbols, such as the % sign, the $ sign, or the comma

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

Page 8: Numeric Variables Visual Basic for Applications 6.

Assigning a Numeric Literal Constant to a Numeric Variable

6

Page 9: Numeric Variables Visual Basic for Applications 6.

Assigning a Numeric Expression to a Numeric Variable

6

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

Page 10: Numeric Variables Visual Basic for Applications 6.

Assigning a Numeric Expression to a Numeric Variable

6

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

Page 11: Numeric Variables Visual Basic for Applications 6.

Examples of Assignment Statements Containing Numeric Expressions

6

Page 12: Numeric Variables Visual Basic for Applications 6.

Summary

To reserve a procedure-level numeric variable: 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

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

6

Page 13: Numeric Variables Visual Basic for Applications 6.

Excel Lesson:Viewing the Paradise Electronics Price List

To open Jake’s workbook and viewthe Paradise Electronics price list, use the steps on pages 338 and 339 of the textbook

6

Page 14: Numeric Variables Visual Basic for Applications 6.

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 Computers worksheet, use the steps on pages 340 to 342 of the textbook

6

Page 15: Numeric Variables Visual Basic for Applications 6.

Control Toolbox Toolbar6

Page 16: Numeric Variables Visual Basic for Applications 6.

Placement of Crosshair6

Page 17: Numeric Variables Visual Basic for Applications 6.

List Box Control Drawn on the Worksheet

6

Page 18: Numeric Variables Visual Basic for Applications 6.

Creating a List Box6

To change the value assigned to several of the list box’s properties, use the steps on pages 343 to 347 of the textbook

The Object box, located immediately below the Properties window’s title bar, displays the name and type of the selected object

Page 19: Numeric Variables Visual Basic for Applications 6.

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

6

Page 20: Numeric Variables Visual Basic for Applications 6.

List Box Shownin the Correct Size

6

Page 21: Numeric Variables Visual Basic for Applications 6.

Coding the List Box’s DblClick Event Procedure

A list box’s DblClick event procedure occurs when the user double-clicks an item in the list

6

Page 22: Numeric Variables Visual Basic for Applications 6.

Variables Used by the List Box’s DblClick Event Procedure

6

Page 23: Numeric Variables Visual Basic for Applications 6.

Coding the List Box’sDblClick Event Procedure

To begin coding the DblClick event procedure, use the steps on pages 348 and 349 of the textbook

6

Page 24: Numeric Variables Visual Basic for Applications 6.

Code Window Showing the Additional Two Lines of Code

6

Page 25: Numeric Variables Visual Basic for Applications 6.

Using the Excel VLookup Function in a Procedure

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

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

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

Page 26: Numeric Variables Visual Basic for Applications 6.

Syntax of theVLookup Function

6

Page 27: Numeric Variables Visual Basic for Applications 6.

Examples of theVLookup Function

6

Page 28: Numeric Variables Visual Basic for Applications 6.

Using the Excel VLookup Function in a Procedure

To complete the DblClick event procedure, use the steps on pages 353 and 354 of the textbook

6

Page 29: Numeric Variables Visual Basic for Applications 6.

Using the Excel VLookup Function in a Procedure

To test the list box’s DblClick event procedure, use the steps on pages 354 and 355of the textbook

6

Page 30: Numeric Variables Visual Basic for Applications 6.

Word Lesson:Coding the

UpdateMembership Procedure

Begin by opening this document and viewing the code template for theUpdateMembership procedure, which already has been inserted into a module

To open Pat’s document and view the UpdateMembership procedure, use the steps on pages 362 to 364 of the textbook

6

Page 31: Numeric Variables Visual Basic for Applications 6.

Membership Document6

Page 32: Numeric Variables Visual Basic for Applications 6.

Pseudocode for the UpdateMembership Procedure

6

Page 33: Numeric Variables Visual Basic for Applications 6.

Variables Used by the UpdateMembership Procedure

6

Page 34: Numeric Variables Visual Basic for Applications 6.

Coding theUpdateMembership Procedure

6

To begin coding the UpdateMembership procedure, use the steps on pages 365 and 366 of the textbook

Page 35: Numeric Variables Visual Basic for Applications 6.

Code Window Showing the Additional Two Lines of Code

6

Page 36: Numeric Variables Visual Basic for Applications 6.

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

To continue coding the

UpdateMembership procedure, use the

steps on pages 366 to 368 of the textbook

6

Page 37: Numeric Variables Visual Basic for Applications 6.

Moving the Insertion Point to the Beginning of the Document

You can use the Selection object’s HomeKey method to move the insertion point to a different area in the document

The HomeKey method, whose syntax is expression.HomeKey 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

To complete and test the UpdateMembership procedure, use the steps on pages 369 and 370 of the textbook

6

Page 38: Numeric Variables Visual Basic for Applications 6.

Completed UpdateMembership Procedure

6

Page 39: Numeric Variables Visual Basic for Applications 6.

Updated Membership Document6

Page 40: Numeric Variables Visual Basic for Applications 6.

Access Lesson:The ADO Object Model

The ADO (ActiveX Data Objects) object model contains allof the objects needed to manage the records contained in one or more tables

6

Page 41: Numeric Variables Visual Basic for Applications 6.

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

6

Page 42: Numeric Variables Visual Basic for Applications 6.

Coding the PaymentUpdate Procedure

Begin by opening this database and viewing the Payments table

To open Professor Martinez’s database and view the Payments table, use the steps on pages 376 to 378 ofthe textbook

6

Page 43: Numeric Variables Visual Basic for Applications 6.

Pseudocode for the PaymentUpdate Procedure

6

Page 44: Numeric Variables Visual Basic for Applications 6.

Variables Used by the PaymentUpdate Procedure

6

Page 45: Numeric Variables Visual Basic for Applications 6.

Coding the PaymentUpdate Procedure6

To begin coding the PaymentUpdate procedure, use the steps on pages 379 and 380 of the textbook

Page 46: Numeric Variables Visual Basic for Applications 6.

Using the Recordset Object’s Open Method

6

You use the Recordset object’s Open method to open a recordset

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

Page 47: Numeric Variables Visual Basic for Applications 6.

Valid Constants for the Open Method’s CursorType and

LockType Arguments

6

Page 48: Numeric Variables Visual Basic for Applications 6.

Using the Recordset Object’s Open Method

The cursortype and locktype arguments in the syntax can be one of the constants

The LockType argument preventsmore than one user from editing a specific record at the same time by locking the record, making it unavailable to other users

To continue coding the PaymentUpdate procedure, use the steps on page 382 of the textbook

6

Page 49: Numeric Variables Visual Basic for Applications 6.

Partially Completed PaymentUpdate Procedure

6

Page 50: Numeric Variables Visual Basic for Applications 6.

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

To complete the PaymentUpdate procedure, use the steps on pages 384 and 385 of the textbook

6

Page 51: Numeric Variables Visual Basic for Applications 6.

Examples of Using the Find Method to Search the rstPays

Recordset

6

Page 52: Numeric Variables Visual Basic for Applications 6.

Completed PaymentUpdate Procedure

6

Page 53: Numeric Variables Visual Basic for Applications 6.

Using the Recordset Object’s Find Method

6

To test the PaymentUpdate procedure, use the steps on page 386 of the textbook