Visual Basic and Databases LAP II ISPGAYA Visual Basic and Databases © LAP II ii Notice This guide was developed for the course, “Visual Basic and Databases,”. It is not intended to be a complete reference to Visual Basic. Consult the Microsoft documentation that comes with your particular version of Visual Basic for detailed reference information. This guide refers to several software and hardware products by their trade names. These references are for informational purposes only and all trademarks are the property of their respective companies. Contents © LAP II iii Visual Basic and Databases Contents 1. Introducing Visual Basic and Databases Preview 1-1 Course Objectives 1-2 Course Requirements 1-3 What is a Database? 1-4 Where Does Visual Basic Fit In? 1-6 Building a Visual Basic Application 1-8 Structure of a Visual Basic Application 1-8 Steps in Developing Application 1-9 Drawing the User Interface and Setting Properties 1-9 Setting Properties of Controls at Design Time 1-13 Setting Properties at Run-Time 1-14 How Names are Used in Object Events 1-14 Writing Code 1-15 Review of Variables 1-15 Visual Basic Data Types 1-15 Variable Declaration 1-16 Example 1. Mailing List Application 1-18 Summary 1-25 2. Introduction to Databases Review and Preview 2-1 Database Structure and Terminology 2-2 Relational Databases 2-3 Sample Relational Database 2-4 Sample Database Structure 2-7 Virtual Database Tables 2-8 Creating a Database 2-10 Summary 2-11 Visual Basic and Databases © LAP II iv 3. Database Connection with the DAO Data Control Review and Preview 3-1 DAO or ADO – What’s the Difference? 3-2 DAO Data Control 3-3 DAO Data Control Properties 3-4 Recordset Object 3-5 Data Bound Controls 3-6 Data Bound Control Properties 3-6 Example 3-1. Accessing the Books Database 3-8 DAO Data Control Events 3-11 DAO Data Control Methods 3-11 DAO Data Control Recordset Properties 3-12 DAO Data Control Recordset Methods 3-13 DAO Data Control Recordset Navigation 3-14 Quick Example 1 - Recordset Navigation 3-15 Summary 3-17 Exercise 3. Northwind Traders Database 3-18 Contents © LAP II v 4. Database Connection with ADO Technology Review and Preview 4-1 ADO Data Control 4-2 ADO Data Control Properties 4-3 ConnectionString Property 4-4 Recordset Object 4-5 Data Bound Controls 4-6 Data Bound Control Properties 4-6 Example 4-1. Accessing the Books Database 4-8 ADO Data Control Events 4-11 ADO Data Control Methods 4-12 ADO Data Control Recordset Properties 4-12 ADO Data Control Recordset Methods 4-13 ADO Data Control Recordset Navigation 4-14 Quick Example 1 - Recordset Navigation 4-15 DAO or ADO – What’s the Difference? 4-17 ADO Data Environment 4-18 Connection Object 4-19 Command Object 4-19 Quick Example 2 – Use of ADO Data Environment 4-20 Data Bound Controls with the Data Environment 4-23 Example 4-2. Drag and Drop Controls 4-24 Recordsets in the Data Environment 4-27 Quick Example 3 – Record Navigation with the Data Environment 4-28 DAO to ADO – ADO to DAO 4-30 Summary 4-32 Exercise 4-1. Northwind Traders Database 4-33 Exercise 4-2. Data Environment with Northwind Traders Database 4-36 Visual Basic and Databases © LAP II vi 5. Database Queries with SQL Review and Preview 5-1 SQL Background 5-2 Basics of SQL 5-3 Where Does SQL Fit In Visual Basic? 5-5 SQL with the DAO Data Control 5-6 Quick Example 1 - SQL with the DAO Data Control 5-7 SQL with the ADO Data Control 5-9 Quick Example 2 - SQL with the ADO Data Control 5-10 SQL with the ADO Data Environment 5-12 Quick Example 3 - SQL with the ADO Data Environment 5-14 Example 5-1. SQL Tester 5-17 A Brief (Hopefully) Interlude for Visual Basic 6 Users 5-18 SELECT/FROM SQL Statement 5-23 ORDER BY Clause 5-25 WHERE Clause 5-28 Single Table WHERE Clause 5-29 Multiple Table WHERE Clause 5-32 INNER JOIN Clause 5-37 OUTER JOIN Clause 5-41 Visual Basic Functions with SQL 5-43 SQL Aggregate Functions 5-45 SQL Construction Tools 5-47 Building SQL Commands in Code 5-51 Example 5-2. Searching the Books Database 5-52 Summary 5-58 Exercise 5. Northwind Traders Database 5-59 Contents © LAP II vii 6. Visual Basic Interface Design Review and Preview 6-1 Interface Design Philosophy 6-2 Example 6-1. Mailing List Revisited 6-4 Visual Basic Standard Controls 6-5 Form Control 6-5 Command Button Control 6-6 Label Control 6-7 Text Box Control 6-8 Check Box Control 6-9 Option Button Control 6-10 Frame Control 6-10 Picture Box Control 6-11 Image Control 6-11 Example 6-2. Authors Table Input Form 6-12 Message Box 6-19 Example 6-3. Authors Table Input Form (Message Box) 6-22 Application State 6-24 Example 6-4. Authors Table Input Form (Application State) 6-26 Entry Validation 6-29 Example 6-5. Authors Table Input Form (Entry Validation) 6-31 Input Validation 6-32 Example 6-6. Authors Table Input Form (Input Validation) 6-34 Error Trapping and Handling 6-37 Example 6-7. Authors Table Input Form (Error Trapping and Handling) 6-42 On-Line Help Systems 6-43 Example 6-8. Authors Table Input Form (On-Line Help Systems) 6-51 Application Testing 6-54 Custom Controls 6-55 Masked Edit Control 6-56 Quick Example 1 – Masked Edit Control 6-57 UpDown Control 6-58 Quick Example 2 – UpDown Control 6-59 Tabbed Dialog Control 6-61 Quick Example 3 – Tabbed Dialog Control 6-62 Toolbar Control 6-63 Quick Example 4 – Toolbar Control 6-64 Data Bound List Control 6-66 Quick Example 5 – Data Bound List Control 6-68 Data Bound Combo Control 6-70 Visual Basic and Databases © LAP II viii 6. Visual Basic Interface Design (continued) Data Bound Grid Control 6-71 Quick Example 6 – Data Bound Grid Control 6-72 Data Bound FlexGrid Control 6-73 Quick Example 7 – Data Bound FlexGrid Control 6-74 Calendar Control 6-75 Quick Example 8 – Calendar Control 6-76 Common Dialog Control 6-77 Quick Example 9 – Common Dialog Control 6-79 Additional ADO Custom Controls 6-81 Hierarchical FlexGrid Control 6-82 Quick Example 10 – Hierarchical FlexGrid Control 6-83 Chart Control 6-84 Quick Example 11 – Chart Control 6-85 Month View Control 6-86 Quick Example 12 – Month View Control 6-87 Date Time Picker Control 6-88 Quick Example 13 – Date Time Picker Control 6-89 Data Repeater Control 6-90 Example 6-9. Data Repeater Control 6-91 Summary 6-96 Exercise 6. Publisher Table Input Form 6-97 Build Interface 6-98 Add Message Box(es) 6-102 Code Application State 6-103 Perform Entry Validation 6-104 Perform Input Validation 6-105 Add Error Trapping and Handling 6-105 Add On-Line Help System 6-106 Application Testing 6-107 Contents © LAP II ix 7. Database Management Review and Preview 7-1 Database Management with the DAO Data Control 7-2 Editing Records with the DAO Data Control 7-3 Example 7-1. Editing Records with the DAO Data Control 7-6 Adding Records with the DAO Data Control 7-13 Example 7-2. Adding Records with the DAO Data Control 7-15 Deleting Records with the DAO Data Control 7-18 Example 7-3. Deleting Records with the DAO Data Control 7-20 Database Management with the ADO Data Control 7-22 Editing Records with the ADO Data Control 7-23 Example 7-4. Editing Records with the ADO Data Control 7-26 Adding Records with the ADO Data Control 7-33 Example 7-5. Adding Records with the ADO Data Control 7-35 Deleting Records with the ADO Data Control 7-38 Example 7-6. Deleting Records with the ADO Data Control 7-40 Database Management with the ADO Data Environment 7-42 Editing Records with the ADO Data Environment 7-43 Example 7-7. Editing Records with the ADO Data Environment 7-45 Adding Records with the ADO Data Environment 7-54 Example 7-8. Adding Records with the ADO Data Environment 7-56 Deleting Records with the ADO Data Environment 7-59 Example 7-9. Deleting Records with the ADO Data Environment 7-61 Finding Records in a Database 7-63 Finding Records with the DAO Data Control 7-64 Quick Example 1 - Finding Records with the DAO Data Control 7-66 Finding Records with the ADO Data Control 7-67 Quick Example 2 - Finding Records with the ADO Data Control 7-69 Finding Records with the ADO Data Environment 7-70 Quick Example 3 - Finding Records with the ADO Data Environment 7-72 Modifying Records in Code 7-73 Quick Example 4 – Accessing Records in Code 7-75 Stopping a Database Application 7-76 Example 7-10 – Stopping a Database Application 7-78 Example 7-11. Authors Table Input Form 7-80 Additional Navigation Capabilities 7-81 Editing Records 7-83 Adding Records 7-87 Deleting Records 7-94 Stopping the Application 7-95 Visual Basic and Databases © LAP II x Visual Basic and Databases 1. Introducing Visual Basic and Databases Preview • In this first chapter, we will do a quick overview of what the course entails. We will discuss what you need to complete the course. We’ll take a brief look at what databases are, where they are used, and how Visual Basic is used with databases. And, we’ll review the Visual Basic development environment and the steps followed to build an application in Visual Basic. [...]... LAP II xii Visual Basic and Databases Course Requirements • An obvious requirement is a Windows-based computer with Windows 95, Windows 98, Windows 2000, Windows Me, Windows NT, or Windows XP installed, as well as Visual Basic The student should be familiar with the basics of using the Windows operating system • Visual Basic and Databases requires some edition of Visual Basic 5 or Visual Basic 6 There... Sub Visual Basic provides the Sub line with its arguments (if any) and the End Sub statement You provide any needed code • Using the three-letter prefix when naming an object and using a meaningful name makes finding appropriate event procedures a simpler task © LAP II xxiv Visual Basic and Databases Writing Code • The last step in building a Visual Basic application is to write code using the BASIC. .. our time looking at local databases, but access with remote databases is addressed • We will look at databases in more depth in the next chapter You will see that databases have their own vocabulary Now, let’s take a look at how Visual Basic fits into the database management system © LAP II Contents xv Where Does Visual Basic Fit In? • For database management, we say our Visual Basic application acts... used by Visual Basic to interact with databases: the DAO (data access object) control and the ADO (ActiveX data object) control Both controls will be discussed in this course You should be aware, however, that the ADO control is available only with Visual Basic 6 • Most examples presented in the course notes are done using the Professional Edition of Visual Basic 6 Hence, if you are using Visual Basic. .. diving into actual coding © LAP II Contents xxxv Visual Basic and Databases 2 Introduction to Databases Review and Preview • In the last chapter, we looked at a database in very general terms We learned that the heart of any Visual Basic database application is the Jet database engine In this chapter, we provide more details into the structure of databases and how they are created We will use a sample... Objectives ⇒ Understand the benefits of using Microsoft Visual Basic to build a ‘front-end’ interface as a database programming tool ⇒ Learn database structure, terminology, and proper database design ⇒ Learn how to connect to a database using the Visual Basic DAO (data access object) control ⇒ Use the ADO (ActiveX data object) data control and data environment to connect to a database (Visual Basic 6 only)... database engine native to Visual Basic is known as the Jet engine It is the same engine used by Microsoft Access for database management Hence, it is primarily used to work with Access databases, but it can also work with others © LAP II xvi • Visual Basic and Databases As mentioned, the Jet engine will save us lots of work An observation that illustrates the power of using Visual Basic as a front-end for... Visual Basic applications that operate as front-ends to databases Research has shown that over half of all Visual Basic applications involve working with databases We will look at how to make our applications into complete database management systems, being able to view, search, modify, add, and/ or delete database information • Before going any further, let’s review the steps in building a Visual Basic. .. application and then build a simple application for practice © LAP II xvii Contents Building a Visual Basic Application • In the remainder of this chapter, we will provide an overview of a Visual Basic application and how the Visual Basic development environment is used to develop an application This should provide you with some idea of what knowledge you need to possess to proceed in this course and introduce... control you have drawn, click the object in the form window and drag it to the new location Release the mouse button • To resize a control, click the object so that it is select and sizing handles appear Use these handles to resize the object © LAP II xxii Visual Basic and Databases Setting Properties of Controls at Design Time • Each form and control has properties assigned to it by default when you . 7-94 Stopping the Application 7-95 Visual Basic and Databases © LAP II x Visual Basic and Databases 1. Introducing Visual Basic and Databases Preview • In this first. as Visual Basic. The student should be familiar with the basics of using the Windows operating system. • Visual Basic and Databases requires some edition of Visual Basic 5 or Visual Basic. Visual Basic and Databases LAP II ISPGAYA Visual Basic and Databases © LAP II ii Notice This guide was developed for the course, Visual Basic