Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Variables Visual Basic for Applications 4.

Similar presentations


Presentation on theme: "String Variables Visual Basic for Applications 4."— Presentation transcript:

1 String Variables Visual Basic for Applications 4

2 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

3 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

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

5 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

6 Reserving a Procedure-level String Variable 4

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

8 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

9 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

10 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

11 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

12 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

13 Using the InputBox Function 4

14 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

15 Examples of String Concatenation 4

16 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

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

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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 Commission Workbook Showing January Worksheet 4

27 Formulas Displayed in the January Worksheet 4

28 Pseudocode for the Commission Workbook’s Open Event Procedure 4

29 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

30 Completed Open Event Procedure 4

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

32 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

33 Partially Completed Facsimile Transmittal Sheet 4

34 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

35 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

36 Using Form Fields 4

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

38 Pseudocode for the FaxTransmittal Macro Procedure 4

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

40 Completed FaxTransmittal Macro Procedure 4

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

42 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

43 Macros Toolbar Added to the Document 4

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

45 Location of Completed Macros Toolbar 4

46 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

47 Pseudocode for the SelectFieldOrder Procedure 4

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

49 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

50 StudentReport Report in Descending Order by the LastName Field 4


Download ppt "String Variables Visual Basic for Applications 4."

Similar presentations


Ads by Google