Using the selection structure (Unit 7) Visual Basic for Applications.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Programming with Microsoft Visual Basic th Edition
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter Four The Selection Structure
Using the Select Case Statement and the MsgBox Function (Unit 8)
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Object Variables Visual Basic for Applications 3.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Working with Numeric Variables (Unit 6) Visual Basic for Applications.
Numeric Variables Visual Basic for Applications 6.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® Excel 2010 © 2011 The McGraw-Hill Companies,
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
XP Tutorial 8 Adding Interactivity with ActionScript.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Random Functions Selection Structure Comparison Operators Logical Operator
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Decisions and Conditions
Chapter 4: Decisions and Conditions
The Selection Structure
Chapter 4: The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Visual Basic – Decision Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
The Data Element.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
The Selection Structure
The Data Element.
Chapter 5: The Selection Structure
Presentation transcript:

Using the selection structure (Unit 7) Visual Basic for Applications

Objectives  In this unit, you will learn how to:  Perform selection using the If…Then...Else statement  Write instructions that use comparison operators and logical operators  Use the UCase function  Use the Nest If…Then…Else statement  Use the PublishObjects collection and PublishObject objects in Excel to publish data to a Web page  Use the TableOfContents collection and the TableOfContents objects in Word

Concept Lesson: Discussing the selection structure  You use the selection structure, also called the decision structure, when you want a procedure to make a decision or comparison and then, based on the result of that decision or comparison, select one of two paths  You can use the VBA If…Then…Else statement to include a selection structure in a procedure Exhibit 7-1: The syntax of the If…Then…Else statement

Using the If…Then…Else Statement  The items appearing in square brackets ([ ]) in the syntax are optional  Words in bold, however, are essential components of the statement  Thus, the words, If, Then, and End, If must be included in the statement  Items in italics indicate where the programmer must supply information pertaining to the current procedure  As mentioned earlier, the If…Then…Else statement’s condition can contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators

Comparison Operators  You use comparison operators, sometimes referred to as relational operators, to compare two values  When a condition contains more than one comparison operator, the comparison operators are evaluated from left to right in the condition  Comparison operators are evaluated after any arithmetic operators

Most Commonly Used Comparison Operators Exhibit 7-2: The most commonly used comparison operators

Comparison Operators Exhibit 7-3: The evaluation steps for a condition containing arithmetic and comparison operators Exhibit 7-4: Some examples of the If…Then…Else statement

UCase Function  As is true in many programming languages, string comparisons in VBA are case sensitive, which means that the uppercase version of a letter is not the same as its lowercase counterpart  One way of handling the string comparison problem is to include the UCase function, whose syntax is UCase(String:=string), in your string comparisons  You also can use the UCase function in an assignment statement

Examples of If…Then…Else Statements Whose Conditions Contain the UCase Function Exhibit 7-5: some examples of If…Then…Else statements whose conditions contain the UCase function

Logical Operators  The two most commonly used logical operators are And and Or  You use the And and Or operators to combine several conditions into one compound condition Exhibit 7-6: The most commonly used logical operators

Logical Operators All compound conditions containing a logical operator will evaluate to an answer of either True or False only Exhibit 7-7: The truth tables for the And and Or logical operators

Examples of If…Then…Else Statements Whose Conditions Contain Logical Operators Exhibit 7-8: Examples of If…Then…Else statements whose conditions contain logical operators

Logical Operators When a condition contains arithmetic, comparison, and logical operators, the arithmetic operators are evaluated first, then the comparison operators are evaluated, and then the logical operators are evaluated

Nesting If…Then…Else Statements A nested If…Then…Else statement is one in which either the Then clause or the Else clause includes yet another If…Then…Else statement Exhibit 7-9: The syntax of a nested If…Then…Else statement

Two Examples of Nested If…Then…Else Statements Exhibit 7-10: Two examples of nested If…Then…Else statements

Summary To use the If…Then…Else statement to code the selection structure:  Use the syntax shown in Exhibit 7-1, where condition can contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators To compare two values:  Use the comparison operators (=, >, =, )

Summary To return the uppercase equivalent of a string:  Use the UCase function, whose syntax is UCase(String:=string) To create a compound condition:  Use the logical operators (And and Or) To nest If…Then…Else statements:  Use the syntax shown in Exhibit 7-10

Excel Lesson: Using the selection structure in Excel View the Calculator worksheet and the code template for the PublishCalculator procedure.

The PublishObjects Collection and PublishObject Objects  Contained within each Workbook object is a PublishObjects collection, made up of individual PublishObject objects  Each PublishObject object represents a workbook item that has been saved to a Web page Exhibit 7-11: The PublishObjects collection and PublishObject objects shown in the Excel object model

The Add Method  The Add method’s SourceType argument identifies the type of data the PublishObject object will represent, and it can be any of the seven intrinsic constants  The Add method’s Filename argument specifies the location and name of the file to which the data will be published  The Add method’s Sheet argument specifies the name of the worksheet that contains the data you want to publish, and the Source argument identifies which item in the worksheet is to be published

Syntax and Two Examples of the PublishObjects Collection’s Add Method Exhibit 7-12: The syntax and two examples of the PublishObjects collection’s Add method

The Add Method  The Add method’s HtmlType argument, which can be any of the four intrinsic constants listed in Exhibit 7-12, specifies whether the published item is interactive or static in the Web page  Interactive data can be manipulated by the user  Static data can be viewed only and can’t be manipulated by the user in any way

XlHtmlType Enumeration [Excel 2007 Developer Reference]

The Add Method  After using the PublishObjects collection’s Add method to create a PublishObject object, you then use the PublishObject object’s Publish method to write the HTML necessary to create the Web page  The syntax of the Publish method is expression.Publish Create:=booleanValue, where expression is a PublishObject object and Create argument is a Boolean value, either True or False, that controls how the data represented by the PublishObject object is saved to the HTML file specified in the Add method’s Filename argument

Procedure for the PublishCalculator Procedure Exhibit 7-13: The pseudocode for the PublishCalculator procedure

Coding the PublishCalculator Procedure  Exhibit 7-13 contains a PublishObject object variable named pubCalc  A PublishObject object represents a workbook item that is saved to a Web page

Word Lesson: Using the selection structure in Word View the personal assessment document.

The TablesOfContents Collection  When you create a table of contents in a Word document, a special code, called a field code, is inserted into the document  The field code contains the instructions used to generate the table of contents, referred to as the field results

The TablesOfContents Collection and TableOfContents Objects  You use the TablesOfContents collection’s Add method to add a TableOfContents object to a document Exhibit 7-14: The TablesOfContents collection and TablefContents objects shown in the Word object model

The Add Method  In the Add method’s syntax, docObject is the name of a Document object variable, and range, which must be a Range object, represents the area where you want the table of contents to appear in the document  After adding a table of contents to a document, you can use the TableOfContents collection’s Format property to format the table of contents to one of the predesigned formats available in Word Exhibit 7-15: The syntax and three examples of the TablesOfContents collection’s Add method

The Format Property  The syntax of the Format property is docObject.TablesOfContents.Format =constant, where docObject is the name of a Document object and constant is one of the intrinsic constants  Each constant represents one of the predesigned formats available for tables of contents in Word Exhibit 7-16: The valid constants for the TablesOfContents collection’s Format property

Coding the CreateToc Procedure Exhibit 7-17: The pseudocode for the CreateToc procedure

Variables Used by the CreateToc Procedure Exhibit 7-18: The variables used by the CreateToc procedure

Access Lesson: Using the selection structure in Access Before modifying the PaymentUpdate procedure, open the Trip database and view the Payments table

Modified Pseudocode for the PaymentUpdate Procedure Exhibit 7-19: The modified pseudocode for the PaymentUpdate procedure