String Variables Visual Basic for Applications 4.

Slides:



Advertisements
Similar presentations
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Advertisements

PROGRAMMING IN VISUAL BASIC PART 1
Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 8: Developing an Excel Application
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
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
The Web Warrior Guide to Web Design Technologies
1.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Chapter 8: String Manipulation
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Using the Select Case Statement and the MsgBox Function (Unit 8)
Chapter 4: The Selection Structure
Object Variables Visual Basic for Applications 3.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Working with Numeric Variables (Unit 6) Visual Basic for Applications.
Numeric Variables Visual Basic for Applications 6.
Chapter 12: How Long Can This Go On?
OCC Network Drives  H:\  P:\ 
The Object Model Visual Basic for Applications 2.
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.
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 9 Macros And Visual Basic For Applications.
Working with Date Variables (Unit 5)
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Making Interactive Programs with Visual Basic .NET
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
Lesson 17 Mail Merge. Overview Create a main document. Create a data source. Insert merge fields into a main document. Perform a mail merge. Use data.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Excel Tutorial 8 Developing an Excel Application
A variable is a name for a value stored in memory.
Chapter 3: Using Variables and Constants
Microsoft Access Illustrated
Microsoft Office Illustrated
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
WEB PROGRAMMING JavaScript.
CHAPTER FOUR VARIABLES AND CONSTANTS
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

String Variables Visual Basic for Applications 4

Objectives n In this tutorial, you will learn how to: n Reserve a String variable n Use an assignment statement to assign a value to a String variable n Use the InputBox function to get information from the user at the keyboard n Concatenate strings n Use the Val function n Use strings in calculations n Use the Option Explicit statement n Use a form field in Word n Create a custom toolbar in Word 4

Concept Lesson: Variables n A numeric variable is a memory cell that can store a number—for example, it can store an employee’s gross pay amount n A Date variable is a memory cell that can store a date, such as a birth date or a hire date n A Boolean variable is a memory cell that can store the Boolean values True and False n A String variable is a memory cell that can store a string, which is zero or more characters enclosed in quotation marks (“”) 4

Reserving a Procedure-level String Variable n Only the procedure containing the Dim statement can use the procedure-level variable, and the variable remains in memory only while that procedure is running n When the procedure ends, VBA removes the procedure-level variable from memory n When creating a String variable, datatype is always the keyword String n The more technical term for a string is string literal constant 4

Reserving a Procedure-level String Variable n Literal refers to the fact that the characters enclosed within the quotation marks should be taken literally n Constant refers to the fact that the string’s value does not change while a procedure is running n Be careful not to confuse a String variable with a string literal constant n When you use the Dim statement to reserve a String variable in memory, VBA automatically initializes the variable to a zero-length string 4

Reserving a Procedure-level String Variable 4

n A zero-length string, often referred to as an empty string, is simply two quotation marks with nothing between them, like this: “” 4

Reserving a Procedure-level String Variable 4 n You should assign a descriptive name to each variable that you reserve n The name should reflect both the variable’s data type and purpose n Recall that one popular naming convention is to have the first three characters in the name represent the data type, and the remainder of the name represent the variable’s purpose n Variable names cannot be longer then 255 characters and they cannot be reserved words, such as Print or MsgBox

Using an Assignment Statement to Assign a Value to a String Variable n Assignment statements are so named because they assign values to the memory cells inside the computer n When you assign a new value to a memory cell, the new value replaces the old value, because a memory cell can store only one value at a time 4

Using the InputBox Function n The InputBox function displays one of VBA’s predefined dialog boxes n The dialog box contains a title, a message, an OK button, a Cancel button, and an input area in which the user can enter information 4

Using the InputBox Function 4 n The message that you display in the dialog box should prompt the user to enter the appropriate information in the input area of the dialog box n The syntax of the InputBox function is InputBox(Prompt:=prompt [, Title:=title] [, Default:=defaultValue]) n Notice that the Title and Default arguments appear in square brackets ([ ]) in the syntax n The standard in Windows is to use sentence capitalization for the prompt, and book title capitalization for the title

Using the InputBox Function 4 n Sentence capitalization means that you capitalize only the first word and any words that are customarily capitalized n Book title capitalization means that you capitalize the first letter in each word, except for articles, conjunctions, and prepositions that do not occur at either the beginning or end of the title n A function is a set of instructions that performs a task and returns a value after the task is done

Using the InputBox Function 4

Concatenating Strings 4 n Connecting (or linking) strings together is called concatenating n In VBA, you concatenate strings with the concatenation operator—the ampersand (&) n When concatenating strings, you must be sure to include a space before and after the concatenation operator n If you do not enter a space before and after the ampersand, VBA will not recognize the ampersand as the concatenation operator n In addition to concatenating strings, you also can use strings in calculations

Examples of String Concatenation 4

Using the Val Function n The Val function converts a string into a number, and it then returns the number n The syntax of the Val function is Val(String:=string) n The string can contain only numbers and the period n In addition to string literal constants, the string in a Val function also can be a String variable n When you use the Val function to convert the contents of a String variable to a number, VBA first creates a temporary copy of the String variable in memory; it then converts the contents of the copy to a number 4

Examples of Using the Val Function to Convert String Literal Constants to Numbers 4

Examples of Using the Val Function to Convert the Contents of String Variables to Numbers 4

The Option Explicit Statement 4 n It is considered poor programming practice to allow VBA to reserve variables “on the fly”—in other words, to reserve variables that you did not declare in a Dim statement—because it makes finding errors in your program more difficult

The Option Explicit Statement n You can use the Option Explicit statement to prevent VBA from reserving variables that you did not explicitly declare, but you must be sure to enter the statement in each of the project’s modules n The Option Explicit statement tells the Visual Basic Editor to display an error message if your code contains the name of an undeclared variable 4

Summary To reserve a procedure-level String variable: n The syntax of the Dim statement is Dim variablename As datatype, where variablename represents the name of the variable, and datatype is the keyword String n Variable names must begin with a letter and can contain only letters, numbers, and the underscore (_) 4

Summary To assign a value to a variable: n Use an assignment statement with the following syntax: variablename = value To get input from the user at the keyboard: n The syntax of the InputBox function is InputBox(Prompt: =prompt [, Title:=title] [, Default:=defaultValue) n The prompt, title, and defaultValue must be enclosed in quotation marks 4

Summary n If the user clicks the OK button in the dialog box, the InputBox function returns a string that corresponds to the value that appears in the dialog box’s input area To concatenate (link together) strings: n Use the concatenation operator, which is the ampersand (&). To ensure the ampersand is recognized as the concatenation operator, be sure to include a space before and after it To convert a string in a number: n Use the Val function, syntax: Val(String:=string). The Val function converts the string into a number 4

Summary To prevent VBA from reserving undeclared variables: n Enter the Option Explicit statement in each module’s General Declarations section n To have the Visual Basic Editor enter the Option Explicit statement automatically in every new module, click Tools on the menu bar and then click Options to display the Options dialog box. Select the Require Variable Declaration check box, which is located on the Editor tab 4

Excel Lesson: Coding the Workbook’s Open Event Procedure n Begin by opening Martin’s commission workbook, which is located in the Tut04\Excel folder on your Data Disk n To open Martin’s workbook and view its formulas, then open the Visual Basic Editor and view the workbook’s Open event procedure, use the steps on pages 226 to 229 of the textbook 4

Commission Workbook Showing January Worksheet 4

Formulas Displayed in the January Worksheet 4

Pseudocode for the Commission Workbook’s Open Event Procedure 4

Coding the Workbook’s Open Event Procedure n To code and test the Open event procedure, use the steps on pages 230 to 233 of the textbook 4

Completed Open Event Procedure 4

January Worksheet After Running the Workbook’s Open Event Procedure 4

Word Lesson: Creating a Facsimile Transmittal Sheet n Begin by opening the T4-WD-1 document, which is located in the Tut04\Word folder on your Data Disk n The document contains Pat’s partially completed facsimile transmittal sheet n To open the T4-WD-1 document, then save the document as Fax, use the steps on pages 238 and 239 of the textbook 4

Partially Completed Facsimile Transmittal Sheet 4

Using Form Fields n A form field is a special area in the document reserved for entering and displaying information n You will use three form fields in the facsimile transmittal sheet n The first form field, which you will name CurrentDate, will display the date n The second form field, which you will name ToNames, will display the recipient’s name and also his or her company name n The third form field, named NumPages, is already included in the document, and displays the number of pages being faxed 4

Using Form Fields n To add the first form field to the Fax document, use the steps on pages 240 to 242 of the textbook 4

Using Form Fields 4

n To add the second form field to the form, use the steps on pages 242 and 243 of the textbook 4

Pseudocode for the FaxTransmittal Macro Procedure 4

Creating the FaxTransmittal Macro Procedure n To code the FaxTransmittal procedure, use the steps on pages 245 to 247 of the textbook 4

Completed FaxTransmittal Macro Procedure 4

Creating the FaxTransmittal Macro Procedure n To test the FaxTransmittal procedure, use the steps on pages 247 to 249 of the textbook 4

Creating a Custom Toolbar and Button n To create a custom toolbar and then add to it a button that represents a macro, use the steps on pages 249 to 252 of the textbook 4

Macros Toolbar Added to the Document 4

Project.Module1.FaxTransmittal Macro Being Dragged to the Macros Toolbar 4

Location of Completed Macros Toolbar 4

Access Lesson: Creating the SelectFieldOrder Procedure n To open Professor Martinez’s database and then view the StudentReport report, use the steps on pages 257 and 258 of the textbook 4

Pseudocode for the SelectFieldOrder Procedure 4

Creating the SelectFieldOrder Procedure n To code the SelectFieldOrder procedure, use the steps on pages 260 to 262 of the textbook 4

Creating the SelectFieldOrderMacro Macro n As you learned in Tutorial 1, you use the Macro window to create an Access macro, which can be run from either the Macro window or the Database window in Access n To create the SelectFieldOrderMacro macro, and then save and run the macro, use the steps on pages 262 and 263 of the textbook 4

StudentReport Report in Descending Order by the LastName Field 4