+ All Categories
Home > Documents > Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Date post: 12-Jan-2016
Category:
Upload: alvin-price
View: 229 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
Working with Numeric Variables (Unit 6) Visual Basic for Applications
Transcript
Page 1: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Working with Numeric Variables(Unit 6)

Visual Basic for Applications

Page 2: 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

Page 3: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 4: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Data Types Used to Reserve Numeric Variables

Exhibit 6-1: The datatypes used to reserve numeric variables

Search for “Data Type Summary” in VBE help box

Page 5: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

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

Assigning a Numeric Literal Constant to a Numeric Variable

A numeric literal constant is simply a number

A numeric literal constant cannot contain aletter, 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 (#),

Page 7: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 8: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 9: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 10: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Examples of Assignment Statements Containing Numeric Expressions

Exhibit 6-6: Some examples of assignment statements containing numeric expression

Page 11: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 12: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Excel Lesson:Using numeric variables in Excel

Open Jake’s workbook and viewthe Paradise Electronics price list.

Page 13: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 14: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Inserting Controls You should enable Developer tab in Office 2007 to

insert Controls There are two types of controls:

Form Controls

ActiveX Controls

Form controls were the original controls and are much more simple

ActiveX controls offer more formatting properties MS does not include ActiveX controls for the Mac

Page 15: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Control Toolbox Toolbar(Search for Overview of Form Controls)

Exhibit 6-7: The Control Toolbox toolbar

Page 16: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 17: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 18: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

List Box Control Drawn on the Worksheet

Exhibit 6-9: The list box drawn on the worksheet

Page 19: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Exhibit 6-10: The pseudocode for the list box’s DblClick event procedure

Page 20: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Variables Used by the List Box’s DblClick Event Procedure

Exhibit 6-11: The variables used by the list box’s DblClick event procedure

Page 21: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 22: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Syntax of the VLookup Function

Exhibit 6-12: The syntax of the Vlookup function

Page 23: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Examples of theVLookup Function

Exhibit 6-13: Some examples of the Vlookup function

Page 24: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Word Lesson:Using numeric variables in Word

Open Pat’s document and view the UpdateMembership procedure.

Page 25: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Pseudocode for the UpdateMembership Macro

Exhibit 6-14: The pseudocode for the Updatemembership procedure

Page 26: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Variables Used by the UpdateMembership Procedure

Exhibit 6-15: The variables used by the UpdateMembership procedure

Page 27: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 28: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 29: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Using numeric variables in AccessThe 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

Exhibit 6-16: A portion of the ADO object model showing the most commonly used ADO objects

Page 30: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 31: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Coding the PaymentUpdate Procedure

Begin by opening this database and viewing the Payments table

Open Professor Martinez’s database and view the Payments table.

Page 32: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Pseudocode for the PaymentUpdate Procedure

Exhibit 6-17: The pseudocode for the PaymentUpdate procedure

Page 33: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Variables Used by the PaymentUpdate Procedure

Exhibit 6-18: The variables used by the PaymentUpdate procedure

Page 34: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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, 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 35: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Valid Constants for the Open Method’s CursorType and LockType Arguments

Exhibit 6-19: The valid constants for CursorType and LockType arguments

Page 36: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 37: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

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

Page 38: Working with Numeric Variables (Unit 6) Visual Basic for Applications.

Examples of Using the Find Method to Search the rstPays Recordset

Exhibit 6-20: Some examples of using the Find method to search the rstPays recordset


Recommended