Using the Select Case Statement and the MsgBox Function (Unit 8)

Slides:



Advertisements
Similar presentations
1.
Advertisements

1 Microsoft Access 2002 Tutorial 9 – Automating Tasks With Macros.
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
Chapter 11: Classes and Objects
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
1.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Automating Tasks With Macros
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
String Variables Visual Basic for Applications 4.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
The Repetition Structure Lecture 11 08/07/06 Dan Rao.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 11 1 Microsoft Office Excel 2003 Tutorial 11 – Importing Data Into Excel.
Using the Visual Basic Editor Visual Basic for Applications 1.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Chapter 8: String Manipulation
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
ASP.NET Programming with C# and SQL Server First Edition
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Object Variables Visual Basic for Applications 3.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
Tutorial 11 Using and Writing Visual Basic for Applications Code
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Miscellaneous Excel Combining Excel and Access. – Importing, exporting and linking Parsing and manipulating data. 1.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Using the selection structure (Unit 7) Visual Basic for Applications.
Working with Numeric Variables (Unit 6) Visual Basic for Applications.
Numeric Variables Visual Basic for Applications 6.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
OCC Network Drives  H:\  P:\ 
Concepts of Database Management Seventh Edition
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.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Working with Date Variables (Unit 5)
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
COMPUTER SKILLS MS-ACCESS. Introduction Access is a piece of software known as a database management system. At its most basic level, it can be used to.
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.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Microsoft Office Illustrated
Making Decisions in a Program
Microsoft Visual Basic 2005: Reloaded Second Edition
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Using the Select Case Statement and the MsgBox Function (Unit 8) Visual Basic for Applications

Objectives In this unit, you will learn how to: Perform selection using the Select Case statement Use a message box to communicate with the user Use the QueryTables collection and QueryTable object in Excel to retrieve external data Use the SQL SELECT statement in Excel, Word, and Access to select records Use the MailMerge object in Word to create labels

Concept Lesson: More on the Selection Structure You might have more than two paths from which a selection structure must choose When a selection structure has several paths from which to choose, it is usually simpler and clearer to use the Case form of the selection structure instead of the nested If form The Case form is sometimes referred to as an extended selection structure in a procedure

The Select Case Statement The Select Case statement begins with the Select Case clause and ends with the End Select clause Between the Select Case and End Select clauses are the individual Case clauses Each Case clause represents a different path that the selection structure can follow The Select Case clause must include a testexpression The testexpression can be any numeric, string, or Boolean expression

The Select Case Statement Each of the individual Case clauses, except the Case Else, must contain an expressionlist, which can include one or more numeric, string, or Boolean expressions The data type of the expressions included in the expressionlists must be compatible with the data type of the testexpression When the Select Case statement is processed, the value of the testexpression is first compared with the values listed in expressionlist1 If a match is found, the instructions for the first Case clause are processed and then the instruction following the End Select clause is processed

Syntax and an Example of the Select Case Statement Exhibit 8-1: The syntax and an example of the Select Case statement

Using To and Is in an Expressionlist You can use either the keyword To or Is to specify a range of values in an expressionlist; the values included in the range can be either numeric or a string When you use the To keyword in a Case clause, the value preceding the To always must be smaller than the value following the To If you neglect to type the keyword Is in an expression, the Visual Basic Editor will type it in for you

Example of Using the To and Is Keywords in a Select Case Statement Exhibit 8-3: An example of using the To and Is keywords in a Select Case statement

The MsgBox Function In addition to the MsgBox statement, VBA also has a MsgBox function The MsgBox function allows you to display a dialog box that contains a message, one or more command buttons, and an icon After displaying the dialog box, both the MsgBox statement and the MsgBox function wait for the user to choose one of the command buttons Unlike the MsgBox statement, the MsgBox function returns a value that indicates which button the user chose

Syntax and Examples of the MsgBox Statement & the MsgBox Function Exhibit 8-4: The syntax and examples of the MsgBox statement and the Msgbox function

The Buttons Argument The buttons argument is an optional numeric expression that represents the sum of values specifying the number and type of buttons to display in the dialog box, the icon style to use, and the identity of the default button If you omit the buttons argument, the dialog box contains an OK button only; it does not contain an icon The buttons argument’s settings are divided into three groups If you do not want to display an icon in the message box, you do not need to include a number from the second group in the buttons argument

Valid Settings for the buttons Argument Exhibit 8-5: The valid settings for the buttons argument

MsgBox Function’s Buttons Exhibit 8-6: The Msgbox function’s buttons

Values Returned by the MsgBox Function If the user selects the Retry button, the MsgBox function returns the integer 4, represented by the intrinsic constant vbRetry Exhibit 8-7: Some examples of using the Msgbox function’s return values

Summary To use the Select Case statement to code the selection structure: Use the syntax shown in Figure 8-1, where the testexpression and the expressionlists can contain a numeric, string, or Boolean expression composed of a combination of variables, constants, functions, and operators To specify a range of values in a Case clause’s expressionlist: Use the keyword To in the following syntax: smallest value in the range TO largest value in the range

Summary Use the keyword Is in the following syntax: Is relational operator value To display VBA’s predefined message box, and then return a value that indicates the button that was selected in the message box: Use the MsgBox function.

Excel Lesson: Viewing the MthSales Database and the Sales Worksheet The Student Data folder contains the Access database that is used to record each store’s monthly sales information The database is named MthSales, and it is stored in the Tut08\Excel folder

Viewing the MthSales Database and the Sales Worksheet To view the workbook: 1. Start Microsoft Excel 2. Open theT8-EX-1 (T8-EX-1.xls) workbook, which is located in the Tut08\Excel folder in the Student Data folder on your hard drive. Click the Enable Macros button, if necessary, then save the workbook as Monthly Sales. The Monthly Sales workbook contains one worksheet, named Sales.

Using Microsoft Query Microsoft Query is a separate program that comes with Microsoft Excel, and it is used to bring data from a database into an Excel worksheet Microsoft Query can retrieve data from the following database sources: Microsoft SQL, Microsoft Access, Microsoft Excel, Oracle, SQL Server, and text file databases

Remarks Using Microsoft Query If you import data using the user interface, data from a Web query or a text query is imported as a QueryTable object, while all other external data is imported as a ListObject object. If you import data using the object model, data from a Web query or a text query must be imported as a QueryTable, while all other external data can be imported as either a ListObject or a QueryTable. http://msdn.microsoft.com/en-us/library/office/ff834459.aspx

The QueryTables Collection and QueryTable Objects Each QueryTable object represents a worksheet table built from data retrieved from an external data source You can use the QueryTable object’s CommandText property to select a different set of records to retrieve from the database and display in the worksheet Exhibit 8-9: The QueryTables collection and QueryTable objects shown in the Excel Object Model

Create a Query Table in VBA Public Sub CreateQueryTable() Dim qtbSales As QueryTable Dim sqlString As String Dim connString As String sqlString = "SELECT * FROM 2002Sales ORDER BY Goal DESC" connString = _ "ODBC;DSN=MS Access Database;DBQ=H:\...\MthSales.mdb" Set qtbSales = ActiveSheet.QueryTables.Add(Connection:=connString, _ Destination:=Range("a3"), Sql:=sqlString) qtbSales.Name = "SalesQuery" qtbSales.Refresh End Sub

External Data Range Properties Dialog Box Exhibit 8-8: The External Data Range Properties dialog box

SQL(Structured Query Language) SQL, pronounced like the word “sequel,” stands for Structured Query Language SQL is a set of commands, or statements, that allow you to access and manipulate the data stored in many database management systems SQL is not case sensitive, but programmers use capital letters for SQL keywords

The SQL SELECT Statement The most commonly used SQL command is the SELECT statement The SELECT keyword allows you to select which database fields (and records) you want to view Separate field's by comma, or use an asterisk (*) to select all fields You can also control the order in which the fields and records appear

SQL SELECT Examples SELECT FirstName, LastName FROM tblStudent; SELECT tblStudent.FirstName, tblStudent.LastName FROM tblStudent; SELECT LastName FROM tblStudent WHERE City = ‘Des Plaines’; SELECT LastName FROM tblStudent WHERE City = ‘Skokie’ OR City= ‘Niles’; SELECT LastName FROM tblStudent WHERE City LIKE ‘D*’; SELECT LastName FROM tblStudent ORDER BY FirstName , City DESC;

Basic Syntax and Examples of the SQL SELECT Statement Exhibit 8-10: The basic syntax and examples of the SQL statement

The SQL SELECT Statement The ORDER BY field part of the syntax—referred to as the ORDER BY clause—allows you to control the order in which the records appear when displayed After assigning a SELECT statement to the CommandText property, you then must use the QueryTable object’s Refresh method to update the query table The Refresh method tells Microsoft Excel to connect to the query table’s data source, execute the query that appears in the CommandText property, and then return the appropriate data to the query table

Creating the DisplaySales Macro Procedure The DisplaySales macro should allow anyone to display, in the Sales worksheet, the sales for either all of the stores on only a specific store It also should calculate the difference between the monthly sales goals and the actual sales made

Pseudocode for the DisplaySales Procedure Exhibit 8-11: The pseudocode for the Display Sales procedure

Word Lesson: Viewing the Data Source and the Main Document The mail merge feature in Word allows you to merge a Word document, called the main document, with the data contained in a data source The data source can be another Word document, an Access database, an Outlook contact list, an Excel worksheet, or some other source Before creating the procedure, open Nancy’s main document, which is a Word document stored on your Data Disk You will use this document to generate the Willowtown Health Club labels

The MailMerge Object You use the MailMerge object’s Destination property to control the destination of the merged documents

The MailMerge Object To select the records to be merged with the main document, you use the syntax mailMergeObject.DataSource.QueryString = query, where query is a string that represents a valid SQL SELECT statement Exhibit 8-12: The valid settings for the MailMerge object’s destination property

The SQL SELECT Statement Different applications handle the capitalization of SQL keywords in different ways In this book, you will be entering all SQL keywords in uppercase letters The full syntax of the SELECT statement contains other clauses and options that are beyond the scope of this book To display records in descending order, add the keyword DESC after field in the ORDER BY clause After selecting the appropriate records to merge, you use the MailMerge object’s Execute method to perform the mail merge

Pseudocode for the GenerateLabels Procedure Exhibit 8-13: The pseudocode for the GenerateLabels procedure

Access Lesson: Creating the DisplayReport Procedure Professor Martinez’s database is stored in the Student Data folder on your hard disk Begin by opening the database and viewing the Payments table To open the Trip database and view the Payments table, double-click on Trip and Payments respectively The database contains one table named Payments and one report named PayReport The Payments table contains five fields: ID, LastName, FirstName, Paid, and Updated

Creating the DisplayReport Procedure The Paid field, which stores the total amount paid by each student, is a Currency field

Pseudocode for the DisplayReport Procedure Exhibit 8-14: The pseudocode for the DisplayReport procedure